无法从 ApplePay PKPaymentAuthorizationViewController 获取电子邮件

Can't get email from ApplePay PKPaymentAuthorizationViewController

我正在使用 ApplePay 并需要电子邮件作为联系信息。当我尝试接收电子邮件时,我收到的电子邮件结果是 "Shipping",我什至不知道它来自哪里。

我在请求中要求填写电子邮件字段,并填写了电子邮件信息。

request.requiredShippingAddressFields = PKAddressField.PostalAddress | PKAddressField.Email

代码如下:

func paymentAuthorizationViewController(controller: PKPaymentAuthorizationViewController!, didAuthorizePayment payment: PKPayment!, completion: ((PKPaymentAuthorizationStatus) -> Void)!) {

    let address: ABRecord! = payment.shippingAddress

    let emails: ABMultiValueRef = ABRecordCopyValue(address, kABPersonEmailProperty).takeRetainedValue() as ABMultiValueRef
    if ABMultiValueGetCount(emails) > 0 {
        let email = ABMultiValueCopyLabelAtIndex(emails, 0).takeRetainedValue()
        NSLog(email)  //Here prints "Shipping"
     }
    ...
 }

这是接收电子邮件的正确位置吗?如果不是,正确的做法是什么?


正在尝试获取 phone 号码 (kABPersonPhoneProperty),结果也打印为 "Shipping"。

您的代码中有一个微妙的 "typo":)。 ABMultiValueCopyLabelAtIndex 复制条目的标签(即 "Shipping")。您需要使用 ABMultiValueCopyValueAtIndex.

的值
  let emails: ABMultiValueRef = ABRecordCopyValue(address, kABPersonEmailProperty).takeRetainedValue() as ABMultiValueRef
  if ABMultiValueGetCount(emails) > 0 {
    let email = ABMultiValueCopyValueAtIndex(emails, 0).takeRetainedValue() as String
    NSLog(email)  //Here prints your email address!
  }

对于 iOS9 和更高版本

let email = payment.shippingContact?.emailAddress