我在 iPhone 5.1 模拟器或更低版本中获取地址簿时遇到了一些问题,此代码在 iPhone 6 模拟器中运行良好。我在一些参考资料中发现只有 ios 6 需要权限,但我的情况不同。通讯录总是不返回联系方式和。有没有人告诉我我需要在项目中做什么或配置。提前谢谢了。
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef arrRefPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFMutableArrayRef mArrRefPeople = CFArrayCreateMutableCopy(kCFAllocatorDefault, CFArrayGetCount(arrRefPeople), arrRefPeople);
CFRelease(arrRefPeople);
CFArraySortValues(mArrRefPeople, CFRangeMake(0, CFArrayGetCount(mArrRefPeople)), (CFComparatorFunction)ABPersonComparePeopleByName, (void *)ABPersonGetSortOrdering());
NSArray *arrChosung = [[NSArray alloc] initWithObjects:@"ㄱ",@"ㄲ",@"ㄴ",@"ㄷ",@"ㄸ",@"ㄹ",@"ㅁ",@"ㅂ",@"ㅃ",@"ㅅ",@" ㅆ",@"ㅇ",@"ㅈ",@"ㅉ",@"ㅊ",@"ㅋ",@"ㅌ",@"ㅍ",@"ㅎ", @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];
NSMutableArray *object = [[NSMutableArray alloc] init];
NSMutableArray *section = [[NSMutableArray alloc] init];
int lengthOfPeople = (int)CFArrayGetCount(mArrRefPeople);
NSLog(@"Length of people %d",lengthOfPeople);
int chosungIndex = -1;
int j=-1;
for(int i=0; i<lengthOfPeople; i++) {
ABRecordRef person = CFArrayGetValueAtIndex(mArrRefPeople, i);
NSMutableDictionary *dicAddress = [[NSMutableDictionary alloc] init];
NSNumber *numPersonId = [NSNumber numberWithInt:(int)ABRecordGetRecordID(person)];
NSString *strName = (NSString *)ABRecordCopyCompositeName(person);
ABMultiValueRef refEmail = ABRecordCopyValue(person, kABPersonEmailProperty);
if (refEmail) {
if (ABMultiValueGetCount(refEmail) > 0) {
CFStringRef email = ABMultiValueCopyValueAtIndex(refEmail, 0);
[dicAddress setObject:email ? (NSString *)email : @"" forKey:@"email"];
if (nil != email) {
CFRelease(email);
}
}
CFRelease(refEmail);
}
if (nil == [dicAddress objectForKey:@"email"]) {
[dicAddress setObject:@"" forKey:@"email"];
}
NSString *strNote = (NSString *)ABRecordCopyValue(person, kABPersonNoteProperty);
[dicAddress setObject:strNote ? strNote : @"" forKey:@"memo"];
[strNote release];
//NSLog(@"%d", [numPersonId integerValue]);
[dicAddress setObject:numPersonId forKey:@"id"];
[dicAddress setObject:strName forKey:@"name"];
[strName release];
ABMultiValueRef addressValues = ABRecordCopyValue(person, kABPersonPhoneProperty);
ABMultiValueRef numbers = ABMultiValueCopyArrayOfAllValues(addressValues);
CFRelease(addressValues);
NSArray *arrPhone = (NSArray *)numbers;
if (nil != [arrPhone objectAtIndex:0]) {
[dicAddress setObject:[arrPhone objectAtIndex:0] forKey:@"hp"];
} else {
[dicAddress setObject:@"" forKey:@"hp"];
}
[object addObject:dicAddress];
[dicAddress release];
if([self chosungIndexWithString:strName] != chosungIndex) {
chosungIndex = (int)[self chosungIndexWithString:strName];
if (-1 == j) {
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:[arrChosung objectAtIndex:chosungIndex], @"chosung", [NSNumber numberWithInt:i+1], @"row", nil];
[section addObject:dic];
[dic release];
} else {
int prevRow = [[[section objectAtIndex:j] objectForKey:@"row"] intValue];
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:[arrChosung objectAtIndex:chosungIndex], @"chosung", [NSNumber numberWithInt:i+1-prevRow], @"row", nil];
[section addObject:dic];
[dic release];
}
j++;
}
[arrPhone release];
}
CFRelease(mArrRefPeople);
_lengthOfPeople = lengthOfPeople;
NSLog(@"Length of people %d",lengthOfPeople);
_arrChosung = arrChosung;
_object = object;
_section = section;
CFRelease(addressBook);
NSLog(@"Completed getting address");