Question

I am using the stripe SDK which creates a view for card details. The code is as follows:

//live key
NSString *cardApiKey;

if([hostType isEqualToString:@"production"])
{
    cardApiKey = @"xxxxxxxxxxxxxxxxxxxxx";
} else {
    cardApiKey = @"xxxxxxxxxxxxxxxxxxxxx";
}

self.stripeView = [[STPView alloc] initWithFrame:CGRectMake(10, _cardDetailsLabel.frame.origin.y+_cardDetailsLabel.frame.size.height+10, 200, 55) andKey:cardApiKey];

[self.canvasView addSubview:self.stripeView];

self.stripeView.delegate = self;

The problem is that the number pad keyboard shows by default. I do not want the numberpad keyboard to pop up everytime i init the stripeview. How can I get around this? Thanks.

Was it helpful?

Solution

Approach 1: Using custom views

You could use custom views rather than STPView which would give you full control over when the keyboard is triggered. You can then just populate an instance of STPCart. The stripe gighub page describes this approach under the section: 'using your own views'.

https://github.com/stripe/stripe-ios

STPCard *card = [[STPCard alloc] init];
card.number = @"4242424242424242";
card.expMonth = 12;
card.expYear = 2020;

Approach 2: Using Stripes STPView implementation

To do it with Stripe's STPView you can use an approach using addTarget on the offending UITextField subclass for the UIControlEventEditingDidBegin event. You can remove it after the view loads as it's no longer required:

// YourViewController.h

#import <UIKit/UIKit.h>
#import "YourViewController.h"

@class STPView;

@interface YourViewController : UIViewController

@property (strong,nonatomic) STPView *stripeView;

@end

// YourViewController.m

#import "YourViewController.h"
#import "STPView.h"
#import "PKView.h"
#import "PKTextField.h"

@interface YourViewController ()
@end

@implementation YourViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Create your STPView instance
    _stripeView = [[STPView alloc] initWithFrame:CGRectMake(15,20,290,55) andKey:@"perishable_key"];
    [self.view addSubview:_stripeView];
    // Add target for editingDidBegin UIControl event
    [_stripeView.paymentView.cardNumberField addTarget:self action:@selector(editingBegan:) forControlEvents:UIControlEventEditingDidBegin];
}

- (void)editingBegan:(id)sender
{
    // Hide the keyboard
    [_stripeView.paymentView.cardNumberField resignFirstResponder];
    // Remove the addTarget
    [_stripeView.paymentView.cardNumberField removeTarget:self action:@selector(editingChanged:) forControlEvents:UIControlEventEditingDidBegin];
}

@end

cardNumberField is an instance of PKTextField (a subclass of UITextField) and the view that gets the focus when the STPView is created.

OTHER TIPS

So, As mentioned in the comment, the behaviour is that the focus automatically goes to the STPView's creditCard textField when it gets initialized. Hence, the keyboard gets triggered when the focus is in the textField. So, the workaround should be that we need to make the textField lose focus when it is added to the view. Try like this:

-(void)viewWillLayoutSubviews
{
    [self.stripeView endEditing:YES]; //This sets whatever textFields inside (though we have only one) lose focus when the self.stripeView added to its parentView.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top