3

我正在尝试从ABRecordRefApple 提供的地址中提取送货地址。我有以下内容,但我的街道总是返回nil

ABMultiValueRef addresses = ABRecordCopyValue(abRecordRef, kABPersonAddressProperty);

for (CFIndex index = 0; index < ABMultiValueGetCount(addresses); index++)
{
    CFDictionaryRef properties = ABMultiValueCopyValueAtIndex(addresses, index);
    NSString *street = [(__bridge NSString *)(CFDictionaryGetValue(properties, kABPersonAddressStreetKey)) copy];
    NSLog(@"street: %@", street);
}

我究竟做错了什么?

即使使用以下内容进行调试:

- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
                  didSelectShippingAddress:(ABRecordRef)customShippingAddress
                                completion:(void (^)(PKPaymentAuthorizationStatus status, NSArray *methods, NSArray *items))completion
{
    NSLog(@"%@", ABRecordCopyValue(customShippingAddress, kABPersonAddressProperty);
    completion(PKPaymentAuthorizationStatusSuccess, ..., ...);
}

我得到这个没有街道:

ABMultiValueRef 0x17227fbc0 with 1 value(s)
    0: Shipping (0x17227fa00) - {
    City = "Marina del Rey";
    Country = "United States";
    State = California;
    ZIP = 90292;
} (0x172447440)

编辑

我在访问姓名和电话属性时也遇到了问题:

NSString *name = (__bridge_transfer NSString *)(ABRecordCopyCompositeName(abRecordRef));

NSString *fname = (__bridge_transfer NSString *)ABRecordCopyValue(abRecordRef, kABPersonFirstNameProperty);
NSString *lname = (__bridge_transfer NSString *)ABRecordCopyValue(abRecordRef, kABPersonFirstNameProperty);

if (!name && fname && lname) name = [NSString stringWithFormat:@"%@ %@", fname, lname]; 
NSLog(@"name: %@", name); // nil

这就是创建 PKPaymentRequest 的方式:

PKPaymentRequest *pr = [[PKPaymentRequest alloc] init];    
[pr setMerchantIdentifier:@"********"];
[pr setCountryCode:@"US"];
[pr setCurrencyCode:@"USD"];
[pr setMerchantCapabilities:PKMerchantCapability3DS];
[pr setSupportedNetworks:@[PKPaymentNetworkAmex, PKPaymentNetworkVisa, PKPaymentNetworkMasterCard]];

[pr setPaymentSummaryItems:[self paymentSummaryItems]];

[pr setRequiredBillingAddressFields:PKAddressFieldAll];
[pr setRequiredShippingAddressFields:PKAddressFieldAll];

[pr setShippingMethods:[self supportedShippingMethods]];
4

1 回答 1

8

事实证明,Apple 在这方面的文档并不是那么好,但问题是在委托回调中总是返回paymentAuthorizationViewController:didSelectShippingAddress:completion:部分地址。修复方法是在回调中设置它:

- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
                       didAuthorizePayment:(PKPayment *)payment
                                completion:(void (^)(PKPaymentAuthorizationStatus))completion
{
    // Use this instead.
    [payment shippingAddress];
}

我还删除了设置所需帐单地址的调用(可能是一个单独的错误)。

于 2014-10-23T00:24:49.947 回答