【问题标题】:Swift Apple Pay Crashes; PKPaymentAuthorizationViewController = nilSwift Apple Pay 崩溃; PKPaymentAuthorizationViewController = nil
【发布时间】:2015-09-16 16:06:24
【问题描述】:

我正在尝试在我的应用中实施 Apple Pay。我已经设置了两次商家 ID,并通过 CocoaPods 安装了 Stripe、Stripe-Apple Pay。每次我尝试在我的设备上运行该应用程序时,它都会崩溃。我添加了断点以尝试了解错误发生的位置,当我访问代码时看到以下内容 applePayController.delegate = self

request PKPaymentRequest    0x00000001700f3180  0x00000001700f3180
applePayController  PKPaymentAuthorizationViewController!   nil None

然后应用程序崩溃,我收到此错误消息

function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded> 
of Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ()).(closure #2)
->  0x100581474 <+44>:  brk    #0x1

谁能帮我弄清楚如何解决这个问题。这是我的代码

import UIKit
import PassKit

class DetailViewController: UIViewController {

let SupportedPaymentNetworks = [PKPaymentNetworkVisa, PKPaymentNetworkMasterCard, PKPaymentNetworkAmex]
let ApplePayMerchantID = "merchant.com.Flokk.FlokkV33"

var currentObject : PFObject?

@IBOutlet var EventName: UILabel!
@IBOutlet var EventTime: UILabel!
@IBOutlet var VenueName: UILabel!
@IBOutlet var Location: UILabel!
@IBOutlet var Description: UILabel!
@IBOutlet var EventImage: PFImageView!
@IBOutlet var PayButton: UIButton!

var ticketValue:NSDecimalNumber = 0.99

@IBAction func purchase(sender: AnyObject) {

    let request = PKPaymentRequest()
    let applePayController = PKPaymentAuthorizationViewController(paymentRequest: request)
    applePayController.delegate = self

//
    presentViewController(applePayController, animated: true, completion: nil)

    request.merchantIdentifier = ApplePayMerchantID
    request.supportedNetworks = SupportedPaymentNetworks
    request.merchantCapabilities = PKMerchantCapability.Capability3DS
    request.countryCode = "US"
    request.currencyCode = "USD"

    request.paymentSummaryItems = [
        PKPaymentSummaryItem(label: "Test Ticket", amount: ticketValue),
        PKPaymentSummaryItem(label: "Flokk Event", amount: ticketValue)
    ]


}


override func viewDidLoad() {
    super.viewDidLoad()

    PayButton.hidden = !PKPaymentAuthorizationViewController.canMakePaymentsUsingNetworks(SupportedPaymentNetworks)

    if let object = currentObject {
        EventName.text = object["EventName"] as? String
        Description.text = object["EventDescription"] as? String
        VenueName.text = object["EventVenueName"] as? String
        Location.text = object["EventLocation"] as? String
        Description.adjustsFontSizeToFitWidth = true

        var initialImage = UIImage(named: "DidNotLoad")
        EventImage.image = initialImage
        if let parseEventPhoto = object["EventImage"] as? PFFile{
            EventImage.file = parseEventPhoto
            EventImage.loadInBackground()
        }

}

        }
}
extension DetailViewController: PKPaymentAuthorizationViewControllerDelegate {
    func paymentAuthorizationViewController(controller: PKPaymentAuthorizationViewController!, didAuthorizePayment payment: PKPayment!, completion: ((PKPaymentAuthorizationStatus) -> Void)!) {
        completion(PKPaymentAuthorizationStatus.Success)
    }

    func paymentAuthorizationViewControllerDidFinish(controller: PKPaymentAuthorizationViewController!) {
        controller.dismissViewControllerAnimated(true, completion: nil)
    }
}

【问题讨论】:

  • 你为什么不检查nilPKPaymentAuthorizationViewController?来自苹果文档If the user can’t make payments on any of the payment request’s supported networks, initialization fails and this method returns nil

标签: iphone swift xcode6 stripe-payments applepay


【解决方案1】:

尝试移动:

let applePayController = PKPaymentAuthorizationViewController(paymentRequest: request)
presentViewController(applePayController, animated: true, completion: nil)

purchase(sender: AnyObject) 函数的末尾。

也就是说,首先创建并配置您的支付请求,然后创建PKPaymentAuthorizationViewController

【讨论】:

    猜你喜欢
    • 2015-10-10
    • 2019-05-08
    • 2015-07-04
    • 2015-08-16
    • 2023-02-08
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多