如何在 Apple Pay PKPaymentAuthorizationViewController 上方显示 UIAlertView?

How do you show an UIAlertView above the Apple Pay PKPaymentAuthorizationViewController?

使用下面的标准 UIAlertView 代码将在 Apple Pay PKPaymentAuthorizationViewController sheet 下方显示警报。

[[[UIAlertView alloc] initWithTitle:@"Payment Error"
                            message:[error localizedDescription]
                           delegate:nil
                  cancelButtonTitle:@"Okay"
                  otherButtonTitles:nil] show];

如何在付款授权上方显示sheet?或者是否有不同的方式来显示 Apple Pay 的错误消息?我想在用户输入无效的送货地址时给出具体的错误消息。

您不能在任何 Remote View Controllers 之上显示 UI 元素,因为它可能会危及系统的安全性。这包括 PKPaymentAuthorizationViewController.

阅读有关远程视图控制器的更多信息here

由于系统安全,您无法在 PKPaymentAuthorizationViewController 上显示 UIAlertView

PKPaymentAuthorizationViewController 的整个 UI 通过 Remote View Controller 呈现。这意味着在您提供的 PKPaymentRequest 之外,不可能以其他方式设置或修改此视图的内容。

并且对于处理 Apple Pay 错误,您必须使用 PKPaymentAuthorizationViewControllerDelegate 委托方法来显示付款已成功完成或有任何错误。

演出PKPaymentAuthorizationViewController, 将支付视图控制器呈现为:

PKPaymentAuthorizationViewController *paymentVC = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
paymentVC.delegate = self;
[self presentViewController:paymentVC animated:true completion:nil];
  • 客户使用 Touch ID 批准购买(或者,如果失败 3 次,通过输入他们的密码)。
  • 指纹图标变成一个微调器,标签为“正在处理”
  • 您的代表收到 paymentAuthorizationViewController(_:didAuthorizePayment:完成:) 回调
  • 您的应用程序与您的付款异步通信 处理器和网站后端实际对这些进行收费 付款详情。完成后,您将调用完成 作为参数给定的处理程序 PKPaymentAuthorizationStatus.success 或 PKPaymentAuthorizationStatus.failure 视结果而定。
  • PKPaymentAuthorizationViewController 微调器动画化为 成功或失败图标。如果成功,将收到通知 来自 PassBook,指示客户信用卡上的费用。
  • 您的代表收到 paymentAuthorizationViewControllerDidFinish(_:) 回调。那就是 负责调用 dismiss(animated:completion:) 来解散 付款屏幕。

- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
                       didAuthorizePayment:(PKPayment *)payment
                                completion:(void (^)(PKPaymentAuthorizationStatus status))completion {

    //=========================================
    //=========================================
    //    Call your api here for charge payment and according to that api result show complition as follow
    //========================================
    //========================================


    // Use your payment processor's SDK to finish charging your customer.
    // When this is done, call:
    completion(PKPaymentAuthorizationStatusSuccess);

    // When this is Payment not completed, call:
//    completion(PKPaymentAuthorizationStatusFailure);

    // When this is Supplied billing address is insufficient or otherwise invalid, call:
//    completion(PKPaymentAuthorizationStatusInvalidBillingPostalAddress);

    // When this is Supplied postal address is insufficient or otherwise invalid, call:
//    completion(PKPaymentAuthorizationStatusInvalidShippingPostalAddress);

    // When this is Supplied contact information is insufficient or otherwise invalid, call:
//    completion(PKPaymentAuthorizationStatusInvalidShippingContact);
}


// Sent to the delegate when payment authorization is finished.  This may occur when
// the user cancels the request, or after the PKPaymentAuthorizationStatus parameter of the
// paymentAuthorizationViewController:didAuthorizePayment:completion: has been shown to the user.
//
// The delegate is responsible for dismissing the view controller in this method.
- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller {
    [self dismissViewControllerAnimated:true completion:nil];
}

iOS11 中有一个新回调

public func paymentAuthorizationController(_ controller: PKPaymentAuthorizationController, didAuthorizePayment payment: PKPayment,
handler completion: (PKPaymentAuthorizationResult) -> Void)

如您所见,处理程序从

completion: (PKPaymentAuthorizationStatus) -> Void)

handler completion: (PKPaymentAuthorizationResult) -> Void)

从 iOS 11 开始,我们将在完成处理程序上得到一个 status 数组 NSErrors

查看 this year's session 了解更多详情。

动作 sheet 是一种特定的警报样式,它出现在对控件或动作的响应中,并提供与当前上下文相关的一组两个或多个选项。使用操作 sheet 让人们启动任务,或在执行可能具有破坏性的操作之前请求确认。在较小的屏幕上,一个动作 sheet 从屏幕底部向上滑动。在较大的屏幕上,操作 sheet 会同时显示为弹出窗口。

如果可以增加清晰度,请提供取消按钮。当用户放弃任务时,取消按钮会增强信心。取消按钮应始终包含在屏幕底部的操作 sheet 中。

突出破坏性的选择。对执行破坏性或危险操作的按钮使用红色,并在操作顶部显示这些按钮 sheet。

避免在操作中启用滚动 sheet。如果某个操作 sheet 有太多选项,人们必须滚动才能看到所有选项。滚动需要额外的时间来做出选择,而且如果不无意中点击按钮就很难做到。

有关开发人员指南,请参阅 UIAlertControllerStyleActionSheet constant in UIAlertController