如何使用Swift从Apple Pay获取电子邮件和电话号码

如何使用Swift从Apple Pay获取电子邮件和电话号码,swift,applepay,Swift,Applepay,我能够成功地从Apple Pay检索所有数据,包括加密的支付数据、名字、姓氏和账单地址。然而,我似乎不知道如何从Apple Pay结账时显示的“联系人”部分检索电话号码和电子邮件地址。我需要检索这些值,以便将它们提交给我的下游支付提供商。请帮助: extension ViewController: PKPaymentAuthorizationViewControllerDelegate { /* USER HAS AUTHORIZED PAYMENT */ func payme

我能够成功地从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"
扩展视图控制器:PKPaymentAuthorizationViewControllerDelegate{
/*用户已授权付款*/
func paymentAuthorizationViewController(控制器:PKPaymentAuthorizationViewController!,DIDAuthorizationPayment付款:PKPayment!,完成:((PKPaymentAuthorizationStatus)->Void)!){
//声明请求参数的字典
var参数=字典()
//从AP获取加密的支付数据
参数[“encryptedPayment_data”]=payment.token.paymentData.base64EncodedStringWithOptions(nil)
let billingAddress:ABRecord=付款。billingAddress
参数[“billTo_firstName”]=ABRecordCopyValue(billingAddress,KabbersonfirstName属性)。将RetainedValue()作为?字符串
参数[“billTo_lastName”]=ABRECORDCYVALUE(billingAddress,kABPersonLastNameProperty)。将RetainedValue()作为?字符串
//从ABRecord格式中提取账单地址并相应分配
让addressProperty:ABMultiValueRef=ABRecordCopyValue(billingAddress,KabPersonalAddressProperty)。将UnrepainedValue()作为ABMultiValueRef
如果让dict:NSDictionary=ab多值CopyValueAtIndex(addressProperty,0)。将UnrepainedValue()作为NSDictionary{
参数[“billTo_street1”]=dict[字符串(KabPersonalAddressStreetKey)]作为字符串
参数[“billTo_city”]=dict[字符串(KabPersonalAddressCityKey)]作为?字符串
参数[“billTo_state”]=dict[String(KabPersonalAddressStateKey)]作为字符串
参数[“billTo_postalCode”]=dict[字符串(KabPersonalAddressZipKey)]作为?字符串
参数[“billTo_country”]=dict[字符串(KabPersonalAddressCountryKey)]作为?字符串/“美国”
}
//不知道如何从Apple Pay获得这些!
参数[“billTo_电子邮件”]=”null@sample.com"
参数[“billTo_phoneNumber”]=“5555”
您可以试试

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”。我将此理解为KabbersoneMailProperty在payment.billingAddress中为零。从Apple Pay返回的电子邮件和电话可能会暴露在哪里?忘了提及我正在将帐单和发货地址设置为required,并且它们确实会在AP签出过程中出现:request.requiredBillingAddressFields=PKAddressField.All、 request.requiredShippingAddressFields=PKAddressField.All;情节变厚…我可以使用您的代码从payment.shippingAddress获取电子邮件,但payment.billingAddress返回零。我想知道苹果是否有意隐瞒这一点?在Apple Pay付款单中,只有一个位置可以输入电子邮件和电话信息,并且这些信息会在nder
shippingAddress
,而不是
billingAddress
。有人能将此转换为目标C吗。