我有以下代码:
NSMutableArray *contacts = [[NSMutableArray alloc]init];
CFErrorRef *error = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
if (error)
}
NSArray *arrayOfPeople = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
它在模拟器上运行并给我预期的结果 - 但是在设备上联系人的数量是0并且没有错误(应用程序奇怪地没有要求我访问地址簿的权限 - 我甚至删除了重新安装应用程序) 任何想法?
提前致谢
答案 0 :(得分:2)
在ios 6上,您需要访问地址簿。
致电ABAddressBookRequestAccessWithCompletion()
以获得许可。
请参阅this和AddressHelper
CFErrorRef error = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (error) {
NSLog(@"error %@", error);
}else if (granted){
// Do what you want with the Address Book
}else{
NSLog(@"permission denied");
}
CFRelease(addressBook);
});