我有以下代码:
NSMutableArray *contacts = [[NSMutableArray alloc]init];
CFErrorRef *error = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
if (error)
}
NSArray *arrayOfPeople = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
它可以在模拟器上运行,并且可以给我带来预期的结果-但是,在设备上,联系人数量为0并且没有错误(该应用奇怪地没有询问我访问地址簿的权限-我什至删除并重新安装了该应用)
有任何想法吗 ?
提前致谢
最佳答案
在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);
});
关于ios - 设备上的ABAddressBookCopyArrayOfAllPeople错误-在模拟器上正确,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13702903/