授予地址簿访问权限后加载视图

load view after address book access granted

我非常困惑...当用户单击我主页上的按钮时,如果我们可以访问他们的联系人,我们会将 table 视图控制器推送到我们的导航控制器上。如果没有,我们请求访问权限,如果获得授权,我们将推送相同的 table 视图控制器。 (目前 tableviewcontroller 只是一个填充物)。奇怪的是,如果我们要求他们访问他们的联系人,当我们按下 table 视图控制器时,它不会显示。它只是停留在当前页面上,即使我检查日志它说视图控制器已添加到导航控制器。

检查联系人访问权限的代码:

-(void) loadContacts {
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusDenied ||
    ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusRestricted){
    //1
    NSLog(@"Denied");
} else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized){
    //2
    NSLog(@"Authorized");
    //this method call ends up in a successful page change.
    [self fillContactTable];

} else{ //ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined
    //3
    ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error) {
        if (!granted){
            //4
            NSLog(@"Just denied");
            return;
        }
        //this still calls the method but the page doesn't actually change in the view.
        NSLog(@"Just authorized");
        [self fillContactTable];
    });
}

}

以及 'fillcontacttable' 的代码:

-(void) fillContactTable {
    NSLog(@"fillcontact");
    ContactViewController * cvc = [self.view.storyboard instantiateViewControllerWithIdentifier:@"contactVC"];
    NSLog(@"%@", self.view.navigationController);
    NSLog(@"%@",[self.view.navigationController viewControllers]);
    [self.view.navigationController pushViewController:cvc animated:NO];
    NSLog(@"%@",[self.view.navigationController viewControllers]);
}

注意:即使失败日志仍然显示:

  2015-08-20 10:35:06.534 findme1[2383:97095] (
    "<MainViewController: 0x7f92b9429170>"
)
2015-08-20 10:35:06.536 findme1[2383:97095] (
    "<MainViewController: 0x7f92b9429170>",
    "<ContactViewController: 0x7f92b96181a0>"
)

但是,我确实在 ContactViewController:viewDidLoad 中进行了检查,这不会在失败时触发,所以加载由于某种原因没有完成?

谢谢

您需要确保您的 UI 代码在主线程上被调用:

ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error) {
    if (!granted){
        //4
        NSLog(@"Just denied");
        return;
    }
    //this still calls the method but the page doesn't actually change in the view.
    NSLog(@"Just authorized");
    dispatch_async(dispatch_get_main_queue(), ^{
        [self fillContactTable];
    });
});