开发者

How to determine the type of an address in AddressBook FrameWork (iOS 4.2)

开发者 https://www.devze.com 2023-01-30 08:56 出处:网络
I´ve one big problem... I plan to write an app that deals wi开发者_JAVA技巧th the users addressbook and it´s addresses. Everything´s fine - except the fact that I´m not able to determine whether t

I´ve one big problem... I plan to write an app that deals wi开发者_JAVA技巧th 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; }


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

0

精彩评论

暂无评论...
验证码 换一张
取 消