【问题标题】:pre-populate ApplePay with address in Swift在 Swift 中使用地址预填充 ApplePay
【发布时间】: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

【问题讨论】:

    标签: ios swift applepay


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2017-11-19
      • 1970-01-01
      • 2013-04-09
      • 2023-03-07
      • 2014-03-13
      • 2014-10-30
      • 2021-01-14
      • 2022-06-14
      • 1970-01-01
      相关资源
      最近更新 更多