NSMutableArray *addressBookTemp = [NSMutableArray array]; ABAddressBookRef addressBooks = ABAddressBookCreate(); CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks); CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);
这段代码在IOS6是可以读取通讯录的,但在IOS7下面就不行,程序没报错,调试时候发现返回的都是nil,有木有朋友遇到这个问题?
找到原因了,原来IOS7需要隐私验证,调用通讯录里面API需要以下代码作为权限认证:
__block BOOL accessGranted = NO; if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) { ABAddressBookRequestAccessWithCompletion(addressBooks, ^(bool granted, CFErrorRef error) { // First time access has been granted, add the contact accessGranted = granted; }); } else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) { // The user has previously given access, add the contact accessGranted = YES; } else { // The user has previously denied access // Send an alert telling user to change privacy setting in settings app }