我正在使用此代码从地址簿中获取所有联系人但我面临iOS 9的问题我无法找到解决方案,如果有人知道,请帮助我。
-(void)getContactsFromAddressBook
{
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (addressBook) {
NSArray *allContacts = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSUInteger i = 0;
self.arrContact = [[NSMutableArray alloc]init];
self.arrOnlyNumber = [[NSMutableArray alloc]init];
for (i = 0; i<[allContacts count]; i++) {
addressContact *addressObj = [[addressContact alloc] init];
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
// Get first and last names
NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
NSString *lastName= (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
if(!firstName) {
firstName = kEMPTY_STRING;
}
if (!lastName) {
lastName=kEMPTY_STRING;
}
addressObj.strFullName = [NSString stringWithFormat:@"%@ %@",firstName,lastName];
CFDataRef imgData = ABPersonCopyImageData(contactPerson);
NSData *imageData = (__bridge NSData *)imgData;
addressObj.imgProfile = [UIImage imageWithData:imageData];
// Get mobile number
ABMultiValueRef phonesRef = ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
if ([self getMobilePhoneProperty:phonesRef]!=nil) {
addressObj.strPhonNo = [self getMobilePhoneProperty:phonesRef];
NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
addressObj.strPhonNo = [[addressObj.strPhonNo componentsSeparatedByCharactersInSet:nonDigitCharacterSet] componentsJoinedByString:kEMPTY_STRING];
addressObj.strPhonNo = [addressObj.strPhonNo substringFromIndex: [addressObj.strPhonNo length] - 10];
[self.arrOnlyNumber addObject:addressObj.strPhonNo];
if ([firstName isEqualToString:@""] && [lastName isEqualToString:@""]) {
addressObj.strFullName = addressObj.strPhonNo;
}
[self.arrContact addObject:addressObj];
}
if(phonesRef) {
CFRelease(phonesRef);
}
}
self.contactViewObj.arrContact = self.arrContact;
//[self.contactViewObj viewWillAppear:YES];
if (self.arrContact.count == 0) {
[[[UIAlertView alloc]initWithTitle:kALERT_MESAGE_TITLE message:kALERT_MESSAGE delegate:nil cancelButtonTitle:nil otherButtonTitles:kALERT_OK, nil]show];
}
}
else
{
NSLog(@"Error");
}
}
- (NSString *)getMobilePhoneProperty:(ABMultiValueRef)phonesRef {
for (int i=0; i < ABMultiValueGetCount(phonesRef); i++) {
CFStringRef currentPhoneLabel = ABMultiValueCopyLabelAtIndex(phonesRef, 0);
CFStringRef currentPhoneValue = ABMultiValueCopyValueAtIndex(phonesRef, 0);
if(currentPhoneLabel) {
if (CFStringCompare(currentPhoneLabel, kABPersonPhoneMobileLabel, 0) == kCFCompareEqualTo) {
return (__bridge NSString *)currentPhoneValue;
}
if (CFStringCompare(currentPhoneLabel, kABHomeLabel, 0) == kCFCompareEqualTo) {
return (__bridge NSString *)currentPhoneValue;
}
}
if(currentPhoneLabel) {
CFRelease(currentPhoneLabel);
}
if(currentPhoneValue) {
CFRelease(currentPhoneValue);
}
}
return nil;
}
我试了大约一整天,但还没有运气。 请就此问题向我提供任何帮助或任何指示。
任何帮助将不胜感激。