【问题标题】:Wait for user to respond to request for addressbook access in iOS?等待用户响应 iOS 中的地址簿访问请求?
【发布时间】:2014-06-21 03:05:45
【问题描述】:

我希望我的应用只请求用户访问联系人通讯录的权限,直到用户访问我应用中的正确功能。我真的不想在应用加载时请求权限。

因此我使用了以下代码:

- (IBAction)importClientsButtonPressed:(id)sender {
// request access to Contacts address book
CFErrorRef addyError = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &addyError);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef addyError) {
    });
}

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
    _importContactsActionSheet = [[UIActionSheet alloc] initWithTitle:@"Import Client from Contacts"
                                                             delegate:self
                                                    cancelButtonTitle:@"Cancel"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"Primary Contact", @"Secondary Contact", nil];
    _importContactsActionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;

    [_importContactsActionSheet showFromRect:self.importClientsButton.frame inView:self.importClientsButton.superview animated:YES];
} else {
    // the user has previously denied access - send alert to user to allow access in Settings app
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Privacy Settings"
                                                    message:@"This app does not have access to your contacts.  You can enable access in Privacy Settings."
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

}

但是,权限对话框不会停止应用程序并等待响应...请求后面的代码会继续运行。结果,我收到一堆乱七八糟的消息。

有没有办法让整个应用程序停止,直到权限请求对话框返回响应?

谢谢!

【问题讨论】:

    标签: ios permissions dialog authorization


    【解决方案1】:

    在你上面的代码中你也有这个:

    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef addyError)  {
        if(granted) {
            //Put here your code
        }
    });
    

    所以最后我会这样写代码:

    - (IBAction)importClientsButtonPressed:(id)sender {
        __weak typeof(self) weakSelf = self;
    
        // request access to Contacts address book
        CFErrorRef addyError = NULL;
        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &addyError);
        if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
            ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef addyError)  {
                if(granted) {
                    [weakSelf openImportContact];
                }
            });
        } else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
            [weakSelf openImportContact];
        } else {
            // the user has previously denied access - send alert to user to allow access in Settings app
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Privacy Settings"
                                                            message:@"This app does not have access to your contacts.  You can enable access in Privacy Settings."
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        }
    }
    
    
    - (void)openImportContact {
        dispatch_async(dispatch_get_main_queue(), ^{
            _importContactsActionSheet = [[UIActionSheet alloc] initWithTitle:@"Import Client from Contacts"
                                                             delegate:self
                                                    cancelButtonTitle:@"Cancel"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"Primary Contact", @"Secondary Contact", nil];
            _importContactsActionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    
            [_importContactsActionSheet showFromRect:self.importClientsButton.frame inView:self.importClientsButton.superview animated:YES];
        });
    }
    

    【讨论】:

    • 但是我必须复制一堆......如果这是第一次并且他们刚刚授予访问权限(如您上面的建议),然后再一次如果之前授予访问权限并且不调用此对话框。对吗?
    • 别忘了最后关闭通讯录;)
    • 真的很接近工作了!但是,openImportContact 方法会因以下错误而死,只有当它是第一次并且用户刚刚授予权限时:*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue performTask:] may only be called from the main thread.' 我认为是因为线程。
    • 没问题,现在看代码。问题是您的代码更改了 UI,因此需要在主线程中执行。但是,用户第一次授权应用程序并输入在另一个线程中执行的块。现在我添加了一个 dispatch_async 块来保证你在主线程中执行代码;)
    • 格拉齐马特奥!现在效果很好!我从中学到了很多东西!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    相关资源
    最近更新 更多