Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Application tried to present a nil modal view controller on target?

Tags:

ios

ios7

applepay

I tried to implement Apple Pay in my app, but I am getting a error such that " Application tried to present a nil modal view controller on target " in the following snippet, can any one tell were I am went wrong.

PKPaymentAuthorizationViewController *vc = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:payment];
vc.delegate = self;
[self presentViewController:vc animated:YES completion:nil];
like image 879
Arun Avatar asked Sep 17 '16 05:09

Arun


3 Answers

Actually it will work on Simulator, but not on real device when Apple Pay IS NOT SUPORTED in the country the device is assigned to. Please consider this.

like image 143
Bartłomiej Semańczyk Avatar answered Nov 12 '22 17:11

Bartłomiej Semańczyk


// once check your controller is it initializing properly or not

     if (!viewController) { 
          [self presentViewController:vc animated:YES completion:nil];
     } else {
       // handle error , may be request is not proper to initializing the    PKPaymentAuthorizationViewController
  }
like image 30
Satyanarayana Avatar answered Nov 12 '22 17:11

Satyanarayana


It's likely that your app's Apple Pay entitlement is not set up correctly.

I've noticed canMakePayments returns YES and canMakePaymentsUsingNetworks: returns NO when the entitlement is not set.

(I've also noticed that they can both return YES when the merchant ID you set on your PKPaymentRequest does not match the merchant ID of your Apple Pay entitlement. In this case, your PKPaymentAuthorizationViewController will be non-nil, but presenting it logs a cryptic error in the console).

So to verify that Apple Pay is configured for your app, ensure that "Apple Pay" is "On" in the Capabilities section of your target settings, and that it has a merchant identifier (which you'll need to set up if you haven't already).

Then either:

  • If using your direct PassKit integration method, ensure that you are setting merchantIdentifier property to the matching merchant identifier in the entitlement.

Please check this code:

if ([PKPaymentAuthorizationViewController canMakePayments]) {
    NSLog(@"Can Make Payments");
    PKPaymentAuthorizationViewController *viewController = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
    if (!viewController) { /* ... Handle error ... */ }
    viewController.delegate = self;
    [self presentViewController:viewController animated:YES completion:nil];
}
else {
    NSLog(@"Can't Make payments");
}

or

refer this url with sample code: https://developer.apple.com/library/content/ApplePay_Guide/CreateRequest.html#//apple_ref/doc/uid/TP40014764-CH3-SW2

like image 1
Bhadresh Kathiriya Avatar answered Nov 12 '22 16:11

Bhadresh Kathiriya