设备上的ABAddressBookCopyArrayOfAllPeople错误 - 就在模拟器上

时间:2012-12-04 12:27:32

标签: ios

我有以下代码:

NSMutableArray *contacts = [[NSMutableArray alloc]init];
CFErrorRef *error = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
if (error)
}
NSArray *arrayOfPeople = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);

它在模拟器上运行并给我预期的结果 - 但是在设备上联系人的数量是0并且没有错误(应用程序奇怪地没有要求我访问地址簿的权限 - 我甚至删除了重新安装应用程序) 任何想法?

提前致谢

1 个答案:

答案 0 :(得分:2)

在ios 6上,您需要访问地址簿。 致电ABAddressBookRequestAccessWithCompletion()以获得许可。 请参阅thisAddressHelper

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);
});