Git Product home page Git Product logo

Comments (3)

cannyboy avatar cannyboy commented on July 25, 2024

this would be useful in conjunction with https://github.com/card-io/card.io-iOS-SDK

from paymentkit.

cannyboy avatar cannyboy commented on July 25, 2024

I just wanted to fill in the card number from the number returned from card-io. Not the CVC or expiry.

I could do this separating a card number string into an array of single character NSStrings, and iterating through the array, and pasting the string up to that point into the cardNumberField

UIPasteboard *pb = [UIPasteboard generalPasteboard];
NSString *originalPasteboard = pb.string;
NSMutableArray *numberArray = [NSMutableArray array];
NSString *str = info.cardNumber;
for (int i = 0; i < [str length]; i++) {
    NSString *ch = [str substringWithRange:NSMakeRange(i, 1)];
    [numberArray addObject:ch];
}
[cell.paymentView.cardNumberField becomeFirstResponder];
cell.paymentView.cardNumberField.text = @"";
for (int i = 0; i < [numberArray count]; i++) {
    float f = (float)i * 0.02;
    NSString *number = [numberArray objectAtIndex:i];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSMutableString *newString = [cell.paymentView.cardNumberField.text mutableCopy];
        [newString appendString:number];
        pb.string = number;
        [cell.paymentView.cardNumberField paste:self];
        pb.string = @"";
        if (i == (numberArray.count-1)) pb.string = originalPasteboard;
    });
}

from paymentkit.

zuthan avatar zuthan commented on July 25, 2024

I solved this by creating a category for PTKView that provides a setCardNumber method and calls the textField:shouldChangeCharactersInRange:replacementString method like so:

PTKView+Programmable.h

@interface PTKView (Programmable)
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)replacementString;

    - (void)setCardNumber:(NSString *)cardNumber;
@end

PTKView+Programmable.m

@implementation PTKView (Programmable)
    - (void)setCardNumber:(NSString *)cardNumber {
        NSRange range = NSMakeRange(0, [self cardNumberField].text.length);
        [self textField:[self cardNumberField] shouldChangeCharactersInRange:range replacementString:cardNumber];
    }
@end

Since I'm using swift, I also had to add #import "PTKView+Programmable.h" to my Bridging-Header.h file.

Then I could simply call self.pkView.setCardNumber(cardNumber) in my PaymentViewController.

Since there are obvious use cases for setting the card fields programmatically (such as Card.io), please add this feature natively.

from paymentkit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.