ABAddressBookRequestAccessWithCompletion没有警告,总是没有

时间:2016-05-31 15:13:20

标签: ios objective-c alert

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

如果我创建一个新项目,这段代码将被编译。

但是在我当前的项目中没有给出警报,并且构建失败并出现以下错误   kABAuthorizationStatusNotDetermined state and granted = nil

1 个答案:

答案 0 :(得分:0)

我无法判断代码中的变量“status”是什么。您可以尝试使用以下代码。

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