我正在寻找我在档案中填入的地址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

最佳答案

经过几个小时的奉献,我能够弄清楚这一点,如下所示:

//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, "[email protected]", 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

08-15 23:55