无法从ApplePay PKPaymentAuthorizationViewController获取电子邮件

时间:2015-02-03 18:10:32

标签: ios swift applepay

我正在使用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"
     }
    ...
 }

这是收到电子邮件的正确位置吗?如果没有,那么正确的方法是什么?


尝试获取电话号码(kABPersonPhoneProperty),结果打印为" Shipping"同样。

2 个答案:

答案 0 :(得分:2)

你有一个微妙的错误"在你的代码:)。 ABMultiValueCopyLabelAtIndex复制条目的标签(即#34; 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!
  }

答案 1 :(得分:2)

适用于iOS9及更新版

let email = payment.shippingContact?.emailAddress