Shali Liu
Shali Liu

Reputation: 1174

Can't get email from ApplePay PKPaymentAuthorizationViewController

I am using ApplePay and requiring email as contact information. When I am trying to get email, the email result I get is "Shipping" which I don't even know where it comes from.

I have required email field in the request, and filled in email information.

request.requiredShippingAddressFields = PKAddressField.PostalAddress | PKAddressField.Email

Here is the code:

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"
     }
    ...
 }

Is this the right place to get email? If not, what is the correct approach?


Trying to get phone number (kABPersonPhoneProperty), the result printed as "Shipping" as well.

Upvotes: 1

Views: 821

Answers (2)

jstn
jstn

Reputation: 1892

For iOS9 and newer

let email = payment.shippingContact?.emailAddress

Upvotes: 2

Erik Kerber
Erik Kerber

Reputation: 5656

You have a subtle "typo" in your code :). ABMultiValueCopyLabelAtIndex copies the label for the entry (which is "Shipping"). You'll want the value using 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!
  }

Upvotes: 2

Related Questions