我正在尝试为我的应用实施 Apple Pay。我有 PKPaymentAuthorizationViewController 试图加载 Apple Pay 视图。如果我的钱包中没有任何卡,则构造函数将这个视图控制器返回为 Nil。所以,我决定引导用户完成他们输入卡片信息的过程。我能够使用
PKPassLibrary* lib = [[PKPassLibrary alloc] init];
[lib openPaymentSetup];
这是我初始化 PKPaymentAuthorizationViewController 的部分。这会在模拟器上返回一个有效的对象来显示视图。但是在没有配置信用卡的真实设备上返回 nil 并运行时异常。下面是初始化代码:
if ([PKPaymentAuthorizationViewController canMakePayments]) {
// init arr
[arr addObject:total];
request.paymentSummaryItems = arr;
PKPaymentAuthorizationViewController *paymentPane = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
paymentPane.delegate = self;
[self presentViewController:paymentPane animated:TRUE completion:nil];
}
这里的数组是 PKPaymentSummaryItem 的有效 NSArray,这就是为什么在模拟器上成功工作的原因。
每次我看到钱包里没有信用卡的用户时,我都需要调用上面的 openPaymentSetup 方法。有没有办法检测到呢?
目前我正在使用
if ( [PKPassLibrary isPassLibraryAvailable] ) {
PKPassLibrary* lib = [[PKPassLibrary alloc] init];
if ([lib passesOfType:PKPassTypePayment].count == 0 ) {
[lib openPaymentSetup];
}
}
但这不起作用,因为我正在查看钱包中的通行证计数。这可能像航空公司的登机牌,或 eventbrite 通行证等。
看着: PKPaymentAuthorizationViewController 呈现为 nil 视图控制器
Apple pay PKPaymentauthorizationViewController 在加载支付请求时总是返回 nil