我已经完成了下面的代码来尝试从通讯录中获取所有联系人的电话号码:
ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *arrayOfPeople =
(__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSUInteger index = 0;
allContactsPhoneNumber = [[NSMutableArray alloc] init];
for(index = 0; index<=([arrayOfPeople count]-1); index++){
ABRecordRef currentPerson =
(__bridge ABRecordRef)[arrayOfPeople objectAtIndex:index];
NSArray *phones =
(__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(
ABRecordCopyValue(currentPerson, kABPersonPhoneProperty));
// Make sure that the selected contact has one phone at least filled in.
if ([phones count] > 0) {
// We'll use the first phone number only here.
// In a real app, it's up to you to play around with the returned values and pick the necessary value.
[allContactsPhoneNumber addObject:[phones objectAtIndex:0]];
}
else{
[allContactsPhoneNumber addObject:@"No phone number was set."];
}
}
但是,它在 iOS 6 中运行良好,但在 iOS 5 中运行良好。它在以下代码中崩溃:
ABRecordRef currentPerson =
(__bridge ABRecordRef)[arrayOfPeople objectAtIndex:index];
输出打印:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'
任何人都有建议为什么它会崩溃?谢谢!