尝试以下步骤:
NSMutableArray *firstNames;
NSMutableArray *LastNames;
NSMutableArray *Mobile_numbers;
NSMutableArray *type_array;
NSMutableArray *firstandlast;
...........
-(void)get_arr
{
ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *arrayOfPeople = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSUInteger index = 0;
firstNames = [[NSMutableArray alloc] init];
Mobile_numbers = [[NSMutableArray alloc] init];
type_array=[[NSMutableArray alloc]init ];
LastNames=[[NSMutableArray alloc]init ];
NSMutableArray *firstandlast;
@try
{
for(index = 0; index<=([arrayOfPeople count]-1); index++)
{
ABRecordRef currentPerson = (__bridge ABRecordRef)[arrayOfPeople objectAtIndex:index];
NSString *type;
ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue(currentPerson, kABPersonPhoneProperty);
for (int i=0; i < ABMultiValueGetCount(phones); i++)
{
//NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phones, i);
////NSLog(@"%@", phone);
mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
//NSLog(@"MOG:%@",mobileLabel);
if([mobileLabel isEqualToString:@"_$!<Mobile>!$_"]||[mobileLabel isEqualToString:@"_$!<Main>!$_"])
{
//NSLog(@"mobile:");
type=@"mobile";
}
else if ([mobileLabel isEqualToString:@"_$!<Work>!$_"])
{
//NSLog(@"Work:");
type=@"Work";
}
else if ([mobileLabel isEqualToString:@"_$!<Home>!$_"])
{
//NSLog(@"Home:");
type=@"Home";
}
else if ([mobileLabel isEqualToString:@"_$!<Other>!$_"] )
{
//NSLog(@"Other:");
type=@"Other";
}
mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
//NSLog(@"GG:%@",mobile);
mobile = [mobile stringByReplacingOccurrencesOfString:@"-"
withString:@""];
mobile = [mobile stringByReplacingOccurrencesOfString:@"("
withString:@""];
mobile = [mobile stringByReplacingOccurrencesOfString:@")"
withString:@""];
mobile = [mobile stringByReplacingOccurrencesOfString:@" "
withString:@""];
[Mobile_numbers addObject:mobile];
[type_array addObject:type];
NSString *currentFirstName = (__bridge_transfer NSString *)ABRecordCopyValue(currentPerson, kABPersonFirstNameProperty);
////NSLog(@"NAME:%@",currentFirstName);
if ([currentFirstName length]!=0)
{
//NSLog(@"NAME:%@",currentFirstName);
[firstNames addObject:currentFirstName];
}
else
{
//NSLog(@"NAME:DUMMY");
currentFirstName=@"";
[firstNames addObject:currentFirstName];
}
NSString *currentLast = (__bridge_transfer NSString *)ABRecordCopyValue(currentPerson, kABPersonLastNameProperty);
////NSLog(@"NAME:%@",currentFirstName);
if ([currentLast length]!=0)
{
//NSLog(@"NAME:%@",currentLast);
[LastNames addObject:currentLast];
}
else
{
//NSLog(@"NAME:DUMMY");
currentLast=@"";
[LastNames addObject:currentLast];
}
NSString *temp_f_l=[NSString stringWithFormat:@"%@ %@",currentFirstName,currentLast];
[firstandlast addObject:temp_f_l];
}
NSLog(@"MOB:%@",Mobile_numbers);
NSLog(@"TYPE:%@",type_array);
NSLog(@"FN:%@",firstNames);
NSLog(@"FN:%@",LastNames);
NSLog(@"FN&LN:%@",firstandlast);
}
}
@catch (NSException *exception)
{
//NSLog(@"CATCH");
}
}
在我的应用程序中。我正在使用此代码将地址簿内容存储到数组。我希望它对你有帮助。