如何从ABPersonRef / ABPerson中获取字符串值?

时间:2014-10-27 23:22:38

标签: ios abaddressbook

使用[PKPayment billingAddress]时会出现此问题,ABRecordRef列为类型ABPersonRef(但更确切地说,类型为{{1}})。

1 个答案:

答案 0 :(得分:0)

NSString *streetAddress;
if (ABMultiValueGetCount(addressMultiValueRef) > 0) {
    CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(addressMultiValueRef, 0);
    streetAddress = (__bridge NSString *)CFDictionaryGetValue(dict, kABPersonAddressStreetKey);
}

NSString *city;
if (ABMultiValueGetCount(addressMultiValueRef) > 0) {
    CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(addressMultiValueRef, 0);
    city = (__bridge NSString *)CFDictionaryGetValue(dict, kABPersonAddressCityKey);
}

NSString *state;
if (ABMultiValueGetCount(addressMultiValueRef) > 0) {
    CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(addressMultiValueRef, 0);
    state = (__bridge NSString *)CFDictionaryGetValue(dict, kABPersonAddressStateKey);
}

NSString *zip;
if (ABMultiValueGetCount(addressMultiValueRef) > 0) {
    CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(addressMultiValueRef, 0);
    zip = (__bridge NSString *)CFDictionaryGetValue(dict, kABPersonAddressCityKey);
}

也可以使用相同的方法获取kABPersonAddressCountryCodeKeykABPersonAddressCountryKey属性。

不要忘记链接AddressBook框架和#import <AddressBook/AddressBook.h>