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

时间:2015-08-20 15:43:25

标签: ios objective-c abaddressbook

我非常困惑......当用户点击主页面上的按钮时,如果我们有权访问他们的联系人,我们会将表格视图控制器推送到我们的导航控制器上。如果没有,我们要求访问,如果被授予,我们推送相同的表视图控制器。 (目前tableviewcontroller只是一个填充程序)。这是奇怪的事情,如果我们要求他们访问他们的联系人,当我们推动它没有出现的表视图控制器。它只是停留在当前页面上,即使我检查日志它说视图控制器已添加到导航控制器。

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

-(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并且这不会触发故障,因此由于某种原因没有加载?

谢谢

1 个答案:

答案 0 :(得分:1)

您需要确保在主线程上调用您的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];
    });
});