如何修复应用程序试图在目标上呈现空模态视图控制器的问题?

5

我尝试在我的应用中实现Apple Pay,但是我遇到了一个错误,错误提示为"Application tried to present a nil modal view controller on target"。请问以下代码段中有哪里出错了。

PKPaymentAuthorizationViewController *vc = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:payment];
vc.delegate = self;
[self presentViewController:vc animated:YES completion:nil];
3个回答

2

实际上,模拟器上可以使用,但是当设备所属的国家不支持Apple Pay时,真实设备上无法使用。请考虑这一点。


2

你的应用程序的Apple Pay权限可能没有正确设置。

我注意到当权限未设置时,canMakePayments会返回YES,而canMakePaymentsUsingNetworks:则返回NO

(我还注意到,当你在PKPaymentRequest上设置的商户ID与你的Apple Pay权限的商户ID不匹配时,它们都可以返回YES。在这种情况下,你的PKPaymentAuthorizationViewController将不为nil,但呈现它会在控制台中记录一个神秘的错误)。

因此,要验证你的应用程序是否已配置Apple Pay,请确保在目标设置的"Capabilities"部分中,“Apple Pay”是“开启”的,并且具有商户标识符(如果你尚未设置,则需要设置)。

然后,要么:

  • 如果使用直接的PassKit集成方法,请确保将merchantIdentifier属性设置为权限中匹配的商户标识符。
请检查这段代码:
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");
}

或者

参考此链接以获取示例代码:https://developer.apple.com/library/content/ApplePay_Guide/CreateRequest.html#//apple_ref/doc/uid/TP40014764-CH3-SW2


@bhardresh 在模拟器中可以运行,但在设备上会崩溃。 - Arun
我需要添加UUID到哪个证书中? - Arun
前往功能->Apple Pay,检查所有包含True或Red的显示。 - Bhadresh Kathiriya
让我们在聊天中继续这个讨论 - Bhadresh Kathiriya

1

// 一旦检查您的控制器是否正确初始化

     if (!viewController) { 
          [self presentViewController:vc animated:YES completion:nil];
     } else {
       // handle error , may be request is not proper to initializing the    PKPaymentAuthorizationViewController
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接