使用Swift中的地址预先填充ApplePay

时间:2022-08-30 10:24:46

I'm looking to populate PKPaymentRequest.billingAddress / shippingAddress with an address I have on file as mentioned here:

我正在寻找PKPaymentRequest.billingAddress / shippingAddress填充我在文件中的地址,如下所述:

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

This requires me to create an ABRecordRef from scratch. Below is my best attempt, but my app is crashing without a helpful error message:

这需要我从头开始创建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 个解决方案

#1


I was able to figure this out after a few more hours of dedication, see below:

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

//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

#1


I was able to figure this out after a few more hours of dedication, see below:

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

//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