2016-05-31 70 views
-3
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus(); 
if (status == kABAuthorizationStatusNotDetermined) { 
    NSLog(@"未知"); 
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL,NULL); 

     ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { 
      if (granted) { 
       NSLog(@"ok"); 
      } else { 
       NSLog(@"nonono,always here"); 
      } 


      CFRelease(addressBook); 
     }); 
    } 

如果我創建一個新項目,此代碼將編譯。ABAddressBookRequestAccessWithCompletion不警告,始終沒有授予

但在我的當前項目中沒有給出警報和構建失敗,出現以下錯誤 kABAuthorizationStatusNotDetermined狀態,並授予=零

回答

0

我不能告訴什麼變量「狀態」是從您的代碼。你可以嘗試下面的代碼。

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) { 
     ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(nil, nil); 
     ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) { 
      if (granted) { 
       //You've got contacts permission 
      } else { 
       //User denied to give permission 
      } 
     }); 
    } 
+0

模擬器8,它會顯示警報;但模擬器9,不警報;在模擬器9上的 –

+0

,不警報並且總是記錄「nonono,總是在這裏」; –

+0

您需要使用CNContactStore在iOS 9上獲取聯繫人。此鏈接可以幫助您https://gist.github.com/willthink/024f1394474e70904728 – riowww