当我在我的iphone联系人中同步facebook联系人时,我的应用程序崩溃了所有电话联系人。应用程序每次在名字上崩溃都会导致访问不良。如果Facebook朋友没有同步,应用程序工作正常。这是我的代码:
+(NSMutableArray *)getAllContacts
{
CFErrorRef *error = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
accessGranted = granted;
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else { // we're on iOS 5 or older
accessGranted = YES;
}
if (accessGranted)
{
#ifdef DEBUG
NSLog(@"Fetching contact info ----> ");
#endif
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
NSMutableArray* items = [NSMutableArray arrayWithCapacity:nPeople];
for (int i = 0; i < nPeople; i++)
{
NSMutableDictionary *contacts = [NSMutableDictionary new];
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
//get First Name and Last Name
NSString *firstName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSString *name=@"";
if ([firstName length]>0 && [lastName length]>0)
name= [NSString stringWithFormat:@"%@ %@",firstName,lastName];
else if ([firstName length]>0 && [lastName length]==0)
name= [NSString stringWithFormat:@"%@",firstName];
else if ([firstName length]==0 && [lastName length]>0)
name= [NSString stringWithFormat:@"%@",lastName];
else
name= @"No Name";
contacts[@"name"]=name;
// get contacts picture, if pic doesn't exists, show standart one
NSData *imgData = (__bridge NSData *)ABPersonCopyImageData(person);
UIImage *image= [UIImage imageWithData:imgData];
if (!image)
image = [UIImage imageNamed:@"profilebase_small.png"];
contacts[@"image"]=image;
//get Phone Numbers
NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];
ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
for(CFIndex i=0;i<ABMultiValueGetCount(multiPhones);i++) {
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
NSString *phoneNumber = (__bridge NSString *) phoneNumberRef;
[phoneNumbers addObject:phoneNumber];
}
contacts[@"numbers"]=phoneNumbers;
//get Contact email
NSMutableArray *contactEmails = [NSMutableArray new];
ABMultiValueRef multiEmails = ABRecordCopyValue(person, kABPersonEmailProperty);
for (CFIndex i=0; i<ABMultiValueGetCount(multiEmails); i++) {
CFStringRef contactEmailRef = ABMultiValueCopyValueAtIndex(multiEmails, i);
NSString *contactEmail = (__bridge NSString *)contactEmailRef;
[contactEmails addObject:contactEmail];
// NSLog(@"All emails are:%@", contactEmails);
}
contacts[@"emails"]=contactEmails;
[items addObject:contacts];
}
return items;
} else
{
#ifdef DEBUG
NSLog(@"Cannot fetch Contacts :( ");
#endif
return NO;
}
}
答案 0 :(得分:2)
替换此行CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);与CFIndex nPeople = CFArrayGetCount(allPeople);