请在下面找到导入iOS地址簿的代码(假设我们需要导入 - 不仅仅是为了满足特定需求而获取它的手柄) - >一些使用该应用程序的人抱怨说,只有15-20个地址是从500个中导入的。当一个这样的人向我们发送他们的地址簿时,我注意到除了15-20个其他地址被标记为iCloud。每个人都没有遇到这个问题。有人可以告诉我们是否需要为iCloud标记条目添加特殊代码?我不这么认为,但确认
- (void)getPersonOutOfAddressBook
{
CFErrorRef error = NULL;
__block BOOL accessGranted = NO;
isAddressBookAccessGranted=NO;
ABAddressBookRef addressBook = nil;
addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (ABAddressBookRequestAccessWithCompletion != NULL)
{
//-- we're on iOS 6
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)
{
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (!granted)
{
isAddressBookAccessGranted = NO;
UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"ERROR", nil) message:NSLocalizedString(@"ENABLE CONTACT ACCESS MESSAGE", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil, nil];
[alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
}
else
{
isAddressBookAccessGranted = YES;
[self loadAddressBook:addressBook];
}
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
{
isAddressBookAccessGranted = YES;
{
[self loadAddressBook:addressBook];
}
}
else if(ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusDenied)
{
isAddressBookAccessGranted = NO;
UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"ERROR", nil) message:NSLocalizedString(@"ENABLE CONTACT ACCESS MESSAGE", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil, nil];
[alertView show];
// deleted some other code here - not related to addr book
}
else
{
//deleted some other code here - not related to addr book
}
}
else
{
//-- we're on iOS 5 or older
accessGranted = YES;
isAddressBookAccessGranted = YES;
[self loadAddressBook:addressBook];
}
}
-(void) loadAddressBook:( ABAddressBookRef)currAddressBook
{
if (currAddressBook != nil)
{
ABRecordRef source = ABAddressBookCopyDefaultSource(currAddressBook);
NSArray *allContacts = (NSArray *)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(currAddressBook, source, kABPersonSortByFirstName);
NSUInteger i = 0;
// if([AppDelegate sharedInstance].ABContactsArray!=nil)
[mTempContactsArray removeAllObjects];
for (i = 0; i < [allContacts count]; i++)
{
Person *person = [[[Person alloc] init] autorelease];
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
int recordID = ABRecordGetRecordID(contactPerson);
person.recordID=[NSString stringWithFormat:@"%d",recordID];
NSLog(@"RecordID = %d\n", recordID);
NSString *firstName = ( NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
NSLog(@"firstName = %@\n", firstName);
NSString *lastName = ( NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
NSLog(@"lastName = %@\n", lastName);
NSString *fullName;
if(!(firstName==nil) && !(lastName==nil))
fullName= [NSString stringWithFormat:@"%@ %@", firstName, lastName];
else if((firstName==nil) && (lastName==nil))
fullName = @"";//[NSString stringWithFormat:NSLocalizedString(@"NO NAME STORED", nil)];
else if (firstName==nil)
fullName=[NSString stringWithFormat:@"%@", lastName];
else if(lastName==nil)
fullName=[NSString stringWithFormat:@"%@", firstName];
person.firstName = firstName;
person.lastName = lastName;
person.fullName = fullName;
person.imageData = ( NSData *) ABPersonCopyImageDataWithFormat(contactPerson, kABPersonImageFormatThumbnail);
NSLog(@"imageData = %@\n", person.imageData);
ABMultiValueRef phones = ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
NSUInteger count=0;
NSUInteger j = 0;
person.numberDetailArray = [[[NSMutableArray alloc]init] autorelease] ;
for (j = 0; j < ABMultiValueGetCount(phones); j++)
{
NSString *contactCategory;
NSString *phone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phones, j);
NSLog(@"phone number = %@\n", phone);
NSString *phoneLabel = (__bridge NSString *) ABMultiValueCopyLabelAtIndex(phones, j);
NSLog(@"phoneLabel = %@\n", phoneLabel);
if([phoneLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
{
contactCategory = @"mobile";
count++;
}
else if([phoneLabel isEqualToString:(NSString *)kABPersonPhoneIPhoneLabel])
{
contactCategory = @"iPhone";
count++;
}
else if([phoneLabel isEqualToString:(NSString *)kABPersonPhoneMainLabel])
{
contactCategory = @"main";
count++;
}
else if ([phoneLabel isEqualToString:@"_$!<Home>!$_"])
{
contactCategory = @"home";
count++;
}
else if ([phoneLabel isEqualToString:@"_$!<Work>!$_"])
{
contactCategory = @"work";
count++;
}
else
{
contactCategory = @"other";
count++;
}
NumberInfo *numberInfo = [[[NumberInfo alloc]init] autorelease];
numberInfo.contactNumberCategory = contactCategory;
numberInfo.contactNumber = phone;
numberInfo.parentID = person.recordID;
//numberInfo.recordType = [NSNumber numberWithInt:kNumberRecord];
[person.numberDetailArray addObject:numberInfo];
if(phone)
CFRelease((__bridge CFTypeRef)(phone));
if(phoneLabel)
CFRelease((__bridge CFStringRef)(phoneLabel));
}
person.phoneNumberCount= [NSString stringWithFormat:@"%d",(int)count];
[mTempContactsArray addObject:person];
if(phones)
CFRelease(phones);
}
if(source)
CFRelease(source);
NSLog(@"count=%d",[mTempContactsArray count]) ;
}
//-- after loading all the contacts, perform the table view operations on main thread
// [self performSelectorOnMainThread:@selector(initializeSharedContactList) withObject:nil waitUntilDone:YES];
}
答案 0 :(得分:7)
您需要遍历所有地址簿来源,而不仅仅是默认地址簿来源。使用ABAddressBookCopyArrayOfAllSources
获取所有来源,然后使用ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering
迭代所有来源并获取联系人。
答案 1 :(得分:1)
您可以使用ABAddressBookCopyArrayOfAllPeople来吸引所有人,而不是遍历源代码。
作为旁注,当你这样做时,你可能会得到看似重复的条目。联系人将这些内容整合到一个统一的联系页面中,但是如果您从Facebook和iCloud同步Bob Smith,那么Bob Smith将会有两个ABRecords - 每个来源一个。