1
我正在使用應用程序,我想要獲取contacts.The問題是,該應用程序只會要求第一次該應用程序想要使用contacts.Even如果我刪除應用程序,清理並再次運行,應用程序不會在下次運行時詢問彈出窗口。我調試了代碼,發現我第一次給出的權限已執行,以解決此問題。以下是我寫的代碼在視圖中加載。允許應用程序使用聯繫人彈出窗口不出現ios7
CFErrorRef *error=nil;
ABAddressBookRef addressbook=ABAddressBookCreateWithOptions(NULL, error);
CFArrayRef allpeople=ABAddressBookCopyArrayOfAllPeople(addressbook);
CFIndex npeople=ABAddressBookGetPersonCount(addressbook);
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
if (granted) {
for (int i = 0; i < npeople; i++)
{
ABRecordRef ref = CFArrayGetValueAtIndex(allpeople, i);
NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonFirstNameProperty));
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonLastNameProperty));
NSLog(@"Name:%@ %@", firstName, lastName); }
// First time access has been granted, add the contact
} else {
// User denied access
// Display an alert telling user the contact could not be added
}
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
for (int i = 0; i < npeople; i++)
{
ABRecordRef ref = CFArrayGetValueAtIndex(allpeople, i);
NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonFirstNameProperty));
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonLastNameProperty));
NSLog(@"Name:%@ %@", firstName, lastName); }
// The user has previously given access, add the contact
}
else {
for (int i = 0; i < npeople; i++)
{
ABRecordRef ref = CFArrayGetValueAtIndex(allpeople, i);
NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonFirstNameProperty));
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonLastNameProperty));
NSLog(@"Name:%@ %@", firstName, lastName); }
// The user has previously denied access
// Send an alert telling user to change privacy setting in settings app
}