ios - 授予地址簿访问权限后加载 View

标签 ios objective-c abaddressbook

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

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

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

关于ios - 授予地址簿访问权限后加载 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32122644/

相关文章:

ios - 测试 iOS 定期订阅购买

objective-c - 如何访问 iOS 中的弱链接框架?

macos - 以编程方式启动 OS X 的联系人应用程序以显示联系人?

ios - 在 ios9 中为 CNContactPickerViewController 设置什么 displayedPropertyKeys

ios - 按字母顺序对 UITableView 内容(地址簿)进行排序

ios - 核心数据调试

ios - 这里 iOS SDK : fine-tuning cluster creation

ios - Swift Firebase 在设置时获取 childByAutoId 键

objective-c - 将日志附加到文件中

ios - UITabBarController显示