我能够成功地从 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"
最佳答案
你可以试试
let emails: ABMultiValueRef = ABRecordCopyValue(billingAddress, kABPersonEmailProperty).takeRetainedValue() as ABMultiValueRef
if ABMultiValueGetCount(emails) > 0 {
let email = ABMultiValueCopyValueAtIndex(emails, 0).takeRetainedValue() as String
NSLog(email)
}
关于swift - 如何使用 Swift 从 Apple Pay 获取电子邮件和电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29908539/