iPhone5.1以下のシミュレータでアドレス帳を取得すると問題が発生します。このコードはiPhone6シミュレータでうまく機能します。いくつかの参考文献で、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");