【问题标题】:How to get email and phone number from Apple Pay using Swift如何使用 Swift 从 Apple Pay 获取电子邮件和电话号码
【发布时间】:2015-07-06 15:52:37
【问题描述】:

我能够从 Apple Pay 成功检索所有数据,包括加密的支付数据、名字、姓氏和账单地址。但是,我似乎无法弄清楚如何从 Apple Pay 结帐期间显示的“联系人”部分中检索 电话号码电子邮件地址。我需要检索这些值,以便将它们提交给我的下游支付提供商。请帮忙:

extension ViewController: PKPaymentAuthorizationViewControllerDelegate {

    /* USER HAS AUTHORIZED PAYMENT */
    func paymentAuthorizationViewController(controller: PKPaymentAuthorizationViewController!, didAuthorizePayment payment: PKPayment!, completion: ((PKPaymentAuthorizationStatus) -> Void)!) {

        //Declare a dictionary of request parameters
        var parameters = Dictionary<String, String>()

        //Get Encrypted Payment Data from AP
        parameters["encryptedPayment_data"] = payment.token.paymentData.base64EncodedStringWithOptions(nil)

        let billingAddress: ABRecord = payment.billingAddress

        parameters["billTo_firstName"] = ABRecordCopyValue(billingAddress, kABPersonFirstNameProperty).takeRetainedValue() as? String
        parameters["billTo_lastName"] = ABRecordCopyValue(billingAddress, kABPersonLastNameProperty).takeRetainedValue() as? String

        //Extract billing address from ABRecord format and assign accordingly
        let addressProperty: ABMultiValueRef = ABRecordCopyValue(billingAddress, kABPersonAddressProperty).takeUnretainedValue() as ABMultiValueRef

        if let dict: NSDictionary = ABMultiValueCopyValueAtIndex(addressProperty, 0).takeUnretainedValue() as? NSDictionary {
            parameters["billTo_street1"] = dict[String(kABPersonAddressStreetKey)] as? String
            parameters["billTo_city"] = dict[String(kABPersonAddressCityKey)] as? String
            parameters["billTo_state"] = dict[String(kABPersonAddressStateKey)] as? String
            parameters["billTo_postalCode"] = dict[String(kABPersonAddressZIPKey)] as? String
            parameters["billTo_country"] = dict[String(kABPersonAddressCountryKey)] as? String //"United States"
        }

        //Can't figure out how to obtain these from Apple Pay!
        parameters["billTo_email"] = "null@sample.com"
        parameters["billTo_phoneNumber"] = "5555555555"

【问题讨论】:

    标签: swift applepay


    【解决方案1】:

    你可以试试

    let emails: ABMultiValueRef = ABRecordCopyValue(billingAddress, kABPersonEmailProperty).takeRetainedValue() as ABMultiValueRef
      if ABMultiValueGetCount(emails) > 0 {
        let email = ABMultiValueCopyValueAtIndex(emails, 0).takeRetainedValue() as String
        NSLog(email)  
      }
    

    【讨论】:

    • 感谢您的回复,然而,第一行代码导致“致命错误:在展开可选值 (lldb) 时意外发现 nil”。我将此解释为意味着 kABPersonEmailProperty 在从 Apple Pay 返回的 payment.billingAddress 中为零。电子邮件和电话还有可能暴露在哪里?
    • 忘了提及我正在将帐单和送货地址设置为必需,并且它们确实出现在 AP 结帐过程中:request.requiredBillingAddressFields = PKAddressField.All;request.requiredShippingAddressFields = PKAddressField.All;
    • 情节变厚了...我可以使用您的代码从 payment.shippingAddress 获取电子邮件,但 payment.billingAddress 返回 nil。我想知道 Apple 是否故意阻止这一点?
    • Apple Pay 支付单只有一处输入邮箱和电话信息,返回的是shippingAddress,而不是billingAddress
    • 任何人都可以把它转换成Objective C吗?
    猜你喜欢
    • 2016-07-05
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多