【问题标题】:How can I get string values out of an ABPersonRef / ABPerson?如何从 ABPersonRef / ABPerson 中获取字符串值?
【发布时间】:2014-10-27 23:22:38
【问题描述】:

这个问题是在使用[PKPayment billingAddress] 时出现的,它被列为ABRecordRef 类型(但更准确地说,是ABPersonRef 类型)。

【问题讨论】:

  • 《iOS 通讯簿编程指南》告诉你如何做到这一点。
  • IMO 不透明且难以找到,AddressBook 的 C API 使用不便且难以理解。
  • @rmaddy,这是否说明了如何从 Passbook (Apple Pay) 获取电子邮件和电话号码。我试过了,但它总是为零:(

标签: ios abaddressbook


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

【讨论】:

  • 如何从 Passbook (Apple Pay) 中获取电子邮件、电话号码等值?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-28
  • 1970-01-01
相关资源
最近更新 更多