Ios7 当手机联系人与ios中的facebook好友联系人同步时,应用程序崩溃
当我在iphone联系人中同步facebook联系人时,我的应用程序在获取所有电话联系人时崩溃。应用程序每次都会因名字而崩溃,导致访问错误。如果facebook好友未同步,应用程序也可以正常工作。以下是我的代码:Ios7 当手机联系人与ios中的facebook好友联系人同步时,应用程序崩溃,ios7,Ios7,当我在iphone联系人中同步facebook联系人时,我的应用程序在获取所有电话联系人时崩溃。应用程序每次都会因名字而崩溃,导致访问错误。如果facebook好友未同步,应用程序也可以正常工作。以下是我的代码: +(NSMutableArray *)getAllContacts { CFErrorRef *error = nil; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error
+(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;
}
}
+(NSMutableArray*)getAllContacts
{
CFErrorRef*错误=nil;
ABAddressBookRef addressBook=ABAddressBookCreateWithOptions(空,错误);
__block BOOL accessgrated=否;
如果(ABAddressBookRequestAccessWithCompletion!=NULL){//我们在iOS 6上
dispatch\u semaphore\u t sema=dispatch\u semaphore\u create(0);
ABAddressBookRequestAccessWithCompletion(addressBook,^(已授予bool,CFErrorRef错误){
accessgrated=已授予;
调度信号量信号(sema);
});
调度信号量等待(永远调度时间);
}
否则{//我们使用的是iOS 5或更高版本
accessgrated=是;
}
如果(已授予访问权限)
{
#ifdef调试
NSLog(@“获取联系信息---->”);
#恩迪夫
ABAddressBookRef addressBook=ABAddressBookCreateWithOptions(空,错误);
ABRecordRef source=ABAddressBookCopyDefaultSource(地址簿);
CFArrayRef allPeople=abAddressBookCopyArrayFallPeopleInSourceWithSortOrdering(addressBook、source、kABPersonSortByFirstName);
CFIndex nppeople=ABAddressBookGetPersonCount(地址簿);
NSMutableArray*items=[NSMutableArray阵列容量:NPEOPE];
for(int i=0;i0&&[lastName length]>0)
name=[NSString stringWithFormat:@“%@%@”,firstName,lastName];
else如果([firstName length]>0&&[lastName length]==0)
名称=[NSString stringWithFormat:@“%@”,firstName];
else如果([firstName length]==0&&[lastName length]>0)
名称=[NSString stringWithFormat:@“%@”,lastName];
其他的
name=@“没有名字”;
联系人[@“name”]=姓名;
//获取联系人图片,如果图片不存在,请显示standart one
NSData*imgData=(uu桥NSData*)ABPersonCopyImageData(个人);
UIImage*image=[UIImage-imageWithData:imgData];
如果(!图像)
image=[UIImage imagename:@“profilebase_small.png”];
联系人[@“图像”]=图像;
//获取电话号码
NSMutableArray*phoneNumbers=[[NSMutableArray alloc]init];
ABMultiValueRef multiPhones=ABRecordCopyValue(person,kABPersonPhoneProperty);
对于(CFIndex i=0;i将此行CFIndex nPeople=ABAddressBookGetPersonCount(addressBook);替换为CFIndex nPeople=CFArrayGetCount(allPeople)你找到这个问题的答案了吗?我也面临着同样的问题。如果你已经解决了这个问题,那么请告诉我答案。将这行CFIndex nPeople=abaddress bookgetpersoncount(addressBook);替换为CFIndex nPeople=CFArrayGetCount(allPeople);谢谢Mawoon,现在它可以工作了。你应该把它作为答案来写。