PKPaymentAuthorizationViewController 显示为 nil 视图控制器

Posted

技术标签:

【中文标题】PKPaymentAuthorizationViewController 显示为 nil 视图控制器【英文标题】:PKPaymentAuthorizationViewController present as nil view controller 【发布时间】:2014-11-18 11:15:14 【问题描述】:

我正在尝试在 this 链接之后将 Apple Pay 集成到我的演示应用中,我正面临 this issue 问题。我已将我的 iphone 6+ 操作系统更新到 8.1.1 版本,但仍然无法正确显示 PKPaymentAuthorizationViewController 并且我收到此错误“应用程序试图在目标上显示一个 nil 模态视图控制器”。请提出一些建议,因为我被困在这个问题上.这是我写的代码:-

PKPaymentRequest *request = [[PKPaymentRequest alloc] init];

request.currencyCode = @"USD";
request.countryCode = @"US";
// This is a test merchant id to demo the capability, this would work with Visa cards only.
request.merchantIdentifier = @"merchant.com.procharge";  // replace with YOUR_APPLE_MERCHANT_ID
request.applicationData = [@"" dataUsingEncoding:NSUTF8StringEncoding];
request.merchantCapabilities = PKMerchantCapability3DS;
request.supportedNetworks = @[PKPaymentNetworkMasterCard, PKPaymentNetworkVisa, PKPaymentNetworkAmex];
request.requiredBillingAddressFields = PKAddressFieldPostalAddress|PKAddressFieldPhone|PKAddressFieldEmail;
request.requiredShippingAddressFields = PKAddressFieldPostalAddress|PKAddressFieldPhone|PKAddressFieldEmail;

///Set amount here
NSString *amountText =  @"0.01"; // Get the payment amount
NSDecimalNumber *amountValue = [NSDecimalNumber decimalNumberWithString:amountText];

PKPaymentSummaryItem *item = [[PKPaymentSummaryItem alloc] init];
item.amount = amountValue;
//item.amount = [[NSDecimalNumber alloc] initWithInt:20];
item.label = @"Test Payment Total";

request.paymentSummaryItems = @[item];

PKPaymentAuthorizationViewController *vc = nil;

// need to setup correct entitlement to make the view to show
@try

    vc = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];


@catch (NSException *e)

    NSLog(@"Exception %@", e);


if (vc != nil)

    vc.delegate = self;
    [self presentViewController:vc animated:YES completion:CompletionBlock];

else

    //The device cannot make payments. Please make sure Passbook has valid Credit Card added.
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"PassKit Payment Error"
                                                    message:NSLocalizedString(@"The device cannot make payment at this time. Please check Passbook has Valid Credit Card and Payment Request has Valid Currency & Apple MerchantID.", @"")
                                                   delegate:nil
                                          cancelButtonTitle:NSLocalizedString(@"OK", @"")
                                          otherButtonTitles:nil];
    [alert show];



感谢和问候

【问题讨论】:

nil 视图控制器意味着您的PKPaymentRequest 对象格式错误并且缺少信息。您可以在这里发布您的代码吗? 我也有同样的问题....请人帮忙。 【参考方案1】:

你实现这些方法来试图呈现视图 - 控制前检查? P>

// Determine whether this device can process payment requests.
// YES if the device is generally capable of making in-app payments.
// NO if the device cannot make in-app payments or if the user is restricted from authorizing payments.
+ (BOOL)canMakePayments;

// Determine whether this device can process payment requests using specific payment network brands.
// Your application should confirm that the user can make payments before attempting to authorize a payment.
// Your application may also want to alter its appearance or behavior when the user is not allowed
// to make payments.
// YES if the user can authorize payments on this device using one of the payment networks supported
// by the merchant.
// NO if the user cannot authorize payments on these networks or if the user is restricted from
// authorizing payments.
+ (BOOL)canMakePaymentsUsingNetworks:(NSArray *)supportedNetworks;

【讨论】:

是否必须重写这两种方法并返回YES ?? 跨度> 不,当你执行这些方法,他们将是响应或没有,这将有助于排除故障,为什么苹果的薪酬可能会在该设备上无法工作。 SPAN> PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:@ [PKPaymentNetworkAmex,PKPaymentNetworkMasterCard,PKPaymentNetworkVisa]返回总是NO ...任何知道为什么??? iphone 6 +的ios 8.1.1 ... 跨度> 不canMakePayments是返回?跨度>

以上是关于PKPaymentAuthorizationViewController 显示为 nil 视图控制器的主要内容,如果未能解决你的问题,请参考以下文章