如何确定 AddressBook FrameWork 中地址的类型 (iOS 4.2)

发布于 2024-10-07 18:59:33 字数 1654 浏览 4 评论 0原文

我有一个大问题......我计划编写一个处理用户地址簿及其地址的应用程序。一切都很好 - 除了我无法确定地址的类型是“工作”、“家庭”还是“其他”这一事实。

有人知道如何获得家庭、工作和其他的标签吗?

预先感谢

鲍里斯

这是我目前正在使用的功能:

    + (void)testing {
 //Get the addressbook
 ABAddressBookRef _addressBookRef = ABAddressBookCreate ();

 //Fetch all contacts
 NSArray* allPeople     = (NSArray *)ABAddressBookCopyArrayOfAllPeople(_addressBookRef);

 //Walk the contacts
 for (id record in allPeople) {
  //Get the contact´s id
  NSInteger recordId   = ABRecordGetRecordID((ABRecordRef)record);

  //Get the contact´s name and company
  NSString* recordName  = (NSString *)ABRecordCopyCompositeName((ABRecordRef)record);
  NSString* recordCompany  = (NSString *)ABRecordCopyValue((ABRecordRef)record, kABPersonOrganizationProperty);

  //Get the contact´s addresses
  CFTypeRef adressesReference = ABRecordCopyValue((ABRecordRef)record, kABPersonAddressProperty);
  NSArray *adressesArray  = (NSArray *)ABMultiValueCopyArrayOfAllValues(adressesReference);
  CFRelease(adressesReference);

  NSLog(@"ID:    %d", recordId);
  NSLog(@"Name:  %@", recordName);
  NSLog(@"Firma: %@", recordCompany);

  for (NSString *adress in adressesArray) {
   NSLog(@"Adresse: %@", adress);
  }

  [adressesArray release];
 }

 CFRelease(_addressBookRef);
 [allPeople release];
 NSLog(@"\n");
}

这是日志输出:

ID:1 姓名:第一个用户 菲尔玛:(空) 地址:{ 城市=罗伊特林根; 国家=德国; 国家/地区代码 = de; Street =“某条街道”; 邮编=23456; {

地址: 城市=罗伊特林根; 国家=德国; 国家/地区代码 = de; 状态=BW; Street = "街道编号 2"; 邮编=98765; }

ID:2 姓名:第二联系人 菲尔玛: 菲尔玛 地址:{ 国家=“美国”; 国家/地区代码 = 我们; 街道=测试; }

I´ve one big problem... I plan to write an app that deals with the users addressbook and it´s addresses. Everything´s fine - except the fact that I´m not able to determine whether the addesse´s type is "work", "home" or "other".

Does anybody know how to get the label for home, work and other?

Thanks in advance

Boris

This is the function I´m using at the moment:

    + (void)testing {
 //Get the addressbook
 ABAddressBookRef _addressBookRef = ABAddressBookCreate ();

 //Fetch all contacts
 NSArray* allPeople     = (NSArray *)ABAddressBookCopyArrayOfAllPeople(_addressBookRef);

 //Walk the contacts
 for (id record in allPeople) {
  //Get the contact´s id
  NSInteger recordId   = ABRecordGetRecordID((ABRecordRef)record);

  //Get the contact´s name and company
  NSString* recordName  = (NSString *)ABRecordCopyCompositeName((ABRecordRef)record);
  NSString* recordCompany  = (NSString *)ABRecordCopyValue((ABRecordRef)record, kABPersonOrganizationProperty);

  //Get the contact´s addresses
  CFTypeRef adressesReference = ABRecordCopyValue((ABRecordRef)record, kABPersonAddressProperty);
  NSArray *adressesArray  = (NSArray *)ABMultiValueCopyArrayOfAllValues(adressesReference);
  CFRelease(adressesReference);

  NSLog(@"ID:    %d", recordId);
  NSLog(@"Name:  %@", recordName);
  NSLog(@"Firma: %@", recordCompany);

  for (NSString *adress in adressesArray) {
   NSLog(@"Adresse: %@", adress);
  }

  [adressesArray release];
 }

 CFRelease(_addressBookRef);
 [allPeople release];
 NSLog(@"\n");
}

And here´s the log output:

ID: 1
Name: The first user
Firma: (null)
Adresse: {
City = Reutlingen;
Country = Germany;
CountryCode = de;
Street = "some street";
ZIP = 23456;
}

Adresse: {
City = Reutlingen;
Country = Germany;
CountryCode = de;
State = BW;
Street = "Street number 2";
ZIP = 98765;
}

ID: 2
Name: The second contact
Firma: Firma
Adresse: {
Country = "United States";
CountryCode = us;
Street = Test;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九公里浅绿 2024-10-14 18:59:33

以下是提取地址簿值的方法:

ABMultiValueRef addresses = ABRecordCopyValue(ref, kABPersonAddressProperty);
    for (CFIndex j = 0; j<ABMultiValueGetCount(addresses);j++){
        CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(addresses, j);
        CFStringRef typeTmp = ABMultiValueCopyLabelAtIndex(addreses, j);
        CFStringRef labeltype = ABAddressBookCopyLocalizedLabel(typeTmp);
        NSString *street = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStreetKey) copy];
        NSString *city = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCityKey) copy];
        NSString *state = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStateKey) copy];
        NSString *zip = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressZIPKey) copy];
        NSString *country = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCountryKey) copy];


        [street release];
        [city release];
        [state release];
        [zip release];
        [country release];
        CFRelease(dict);
        CFRelease(type);
        CFRelease(typeTmp);
    }
        CFRelease(addresses);

标签类型就是您要查找的类型。

祝你好运
沙尼

here is how you get the address book values extracted:

ABMultiValueRef addresses = ABRecordCopyValue(ref, kABPersonAddressProperty);
    for (CFIndex j = 0; j<ABMultiValueGetCount(addresses);j++){
        CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(addresses, j);
        CFStringRef typeTmp = ABMultiValueCopyLabelAtIndex(addreses, j);
        CFStringRef labeltype = ABAddressBookCopyLocalizedLabel(typeTmp);
        NSString *street = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStreetKey) copy];
        NSString *city = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCityKey) copy];
        NSString *state = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStateKey) copy];
        NSString *zip = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressZIPKey) copy];
        NSString *country = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCountryKey) copy];


        [street release];
        [city release];
        [state release];
        [zip release];
        [country release];
        CFRelease(dict);
        CFRelease(type);
        CFRelease(typeTmp);
    }
        CFRelease(addresses);

the label type is what you are looking for.

good luck
shani

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文