在 Swift 中使用地址预填充 ApplePay

Posted

技术标签:

【中文标题】在 Swift 中使用地址预填充 ApplePay【英文标题】:pre-populate ApplePay with address in Swift 【发布时间】:2015-05-08 02:43:52 【问题描述】:

我希望使用此处提到的存档地址填充PKPaymentRequest.billingAddress / shippingAddress

https://developer.apple.com/library/ios/documentation/PassKit/Reference/PKPaymentRequest_Ref/#//apple_ref/occ/instp/PKPaymentRequest/billingAddress

这需要我从头开始创建一个ABRecordRef。以下是我的最佳尝试,但我的应用程序在没有有用的错误消息的情况下崩溃:

var request = PKPaymentRequest();

var person: ABRecordRef = ABPersonCreate().takeRetainedValue()
ABRecordSetValue(person, kABPersonFirstNameProperty, "John", nil)
ABRecordSetValue(person, kABPersonLastNameProperty, "Doe", nil)

var multiValue : ABMutableMultiValue = ABMultiValueCreateMutable(ABPropertyType(kABPersonAddressProperty)).takeRetainedValue()
ABMultiValueAddValueAndLabel(multiValue, "123 Test Street", kABPersonAddressStreetKey, nil)
ABMultiValueAddValueAndLabel(multiValue, "Mountain View", kABPersonAddressCityKey, nil)
ABMultiValueAddValueAndLabel(multiValue, "CA", kABPersonAddressStateKey, nil)
ABMultiValueAddValueAndLabel(multiValue, "94040", kABPersonAddressZIPKey, nil)

ABRecordSetValue(person, kABPersonAddressProperty, multiValue, nil)

request.shippingAddress = person
request.billingAddress = person

【问题讨论】:

【参考方案1】:

经过几个小时的努力,我终于弄明白了,见下文:

//Initialize new PassKit Payment Request
var request = PKPaymentRequest();

//Initialize ABRecord to pre-populate AP payment sheet
var record: ABRecordRef = ABPersonCreate().takeRetainedValue()

//Pre-populate first & last name
ABRecordSetValue(record, kABPersonFirstNameProperty, "John", nil)
ABRecordSetValue(record, kABPersonLastNameProperty, "Doe", nil)

//Pre-populate phone
var phone: ABMultiValueRef = ABMultiValueCreateMutable(ABPropertyType(kABMultiStringPropertyType)).takeRetainedValue()
ABMultiValueAddValueAndLabel(phone, "(555)123-4567", kABHomeLabel, nil)
ABRecordSetValue(record, kABPersonPhoneProperty, phone, nil)

//Pre-populate email
var email: ABMultiValueRef = ABMultiValueCreateMutable(ABPropertyType(kABMultiStringPropertyType)).takeRetainedValue()
ABMultiValueAddValueAndLabel(email, "john.doe@test.com", kABHomeLabel, nil)
ABRecordSetValue(record, kABPersonEmailProperty, email, nil)

//Pre-populate address
var address: ABMultiValueRef = ABMultiValueCreateMutable(ABPropertyType(kABMultiDictionaryPropertyType)).takeRetainedValue()
var addressDictionary : [String:String] = [kABPersonAddressStreetKey as String: "123 Test Street", kABPersonAddressCityKey as String: "Palo Alto", kABPersonAddressStateKey as String: "CA", kABPersonAddressZIPKey as String: "94301", kABPersonAddressCountryKey as String: "United States"]
ABMultiValueAddValueAndLabel(address, addressDictionary, kABOtherLabel, nil)
ABRecordSetValue(record, kABPersonAddressProperty, address, nil)

//Assign ABRecord to PKPaymentRequest
request.shippingAddress = record
request.billingAddress = record

【讨论】:

以上是关于在 Swift 中使用地址预填充 ApplePay的主要内容,如果未能解决你的问题,请参考以下文章

预填充Google地图自动填充功能(iOS / Swift)

使用 MagicalRecord 预填充数据库 (swift)。 -wal 日志创建,但是,数据未复制到数据库

Swift - 设备上不包含预填充的 SQLite 数据库

swift 3中Passkit和Applepay之间的区别?

swift Swift - CloudKit - 预填充数据库

使用 SwiftUI 2.0 和 Core Data 预填充数据