2012-01-23 9 views

答えて

7

としてあなたはすべての連絡先のアドレスを取得することができます - 私はこの質問は古いですが、ここsaadnibの回答に基づいてされ、編集されたバージョンである知っている

ABAddressBookRef addressBook = ABAddressBookCreate(); 

NSArray *contactArr = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook); 

for (int i = 0; i < [contactArr count]; i++) 
{ 
    ABRecordRef person = (ABRecordRef)[contactArr objectAtIndex:i]; 

    ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty); 

    for(CFIndex j = 0; j < ABMultiValueGetCount(address); j++) 
    { 
     CFDictionaryRef addressDict = ABMultiValueCopyValueAtIndex(address, j); 

     CFStringRef streetValue = CFDictionaryGetValue(addressDict, kABPersonAddressStreetKey); 

     CFStringRef cityValue = CFDictionaryGetValue(addressDict, kABPersonAddressCityKey); 

     CFStringRef stateValue = CFDictionaryGetValue(addressDict, kABPersonAddressStateKey); 

     CFStringRef zipValue = CFDictionaryGetValue(addressDict, kABPersonAddressZIPKey); 

     CFStringRef countryValue = CFDictionaryGetValue(addressDict, kABPersonAddressCountryKey); 

    } 

} 
+0

ありがとうございました。 –

0

。 ARCに従い、認可ステータスを尊重します。 このメソッドは、1つまたは複数のアドレス(IDを介して接続されている)とともに人物の名前を含むNSMutableDictionaryを返します。

- (NSMutableDictionary *)MyGetAddressesAndNamesOfContacts 
{ 
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusDenied) 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No permission" message:@"This App has no permission to access your contacts." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
     [alert show]; 

     return nil; 
    } 

    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Why this App needs your contacts" message:@"In the following your device will ask you whether this App is allowed to access your contacts. This is recommented because..." delegate:self cancelButtonTitle:@"I understand" otherButtonTitles: nil]; 
     [alert show]; 
    } 

    ABAddressBookRef addressBook = ABAddressBookCreate();  // deprecated since iOS 6 
    NSArray *contactArr = (NSArray *)CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook)); 
    NSMutableDictionary *dPersons = [[NSMutableDictionary alloc] init]; 

    for (int i = 0; i < [contactArr count]; i++) 
    { 
     ABRecordRef person = (ABRecordRef)CFBridgingRetain([contactArr objectAtIndex:i]); 
     NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty); 
     NSString *lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty); 
     NSString *sPersonName = [NSString stringWithFormat:@"%@ %@", firstName, lastName]; 

     ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty); 
     NSString *sAddress; 

     for(CFIndex j = 0; j < ABMultiValueGetCount(address); j++) 
     { 
      CFDictionaryRef addressDict = ABMultiValueCopyValueAtIndex(address, j); 

      CFStringRef streetValue = CFDictionaryGetValue(addressDict, kABPersonAddressStreetKey); 
      CFStringRef cityValue = CFDictionaryGetValue(addressDict, kABPersonAddressCityKey); 
      CFStringRef stateValue = CFDictionaryGetValue(addressDict, kABPersonAddressStateKey); 
      CFStringRef zipValue = CFDictionaryGetValue(addressDict, kABPersonAddressZIPKey); 
      CFStringRef countryValue = CFDictionaryGetValue(addressDict, kABPersonAddressCountryKey); 

      sAddress = [NSString stringWithFormat:@"%@ %@, %@", streetValue, cityValue, countryValue]; 
     [dPersons setObject:sAddress forKey: [NSString stringWithFormat:@"%@%d %@%ld", @"AddressFromNameID", i, @"Number", j]]; 
     } 

     [dPersons setObject:sPersonName forKey: [NSString stringWithFormat:@"%@%d", @"NameWithID", i]]; 
    } 

    return dPersons; 
}