我非常困惑...当用户单击我主页上的按钮时,如果我们可以访问他们的联系人,我们会将 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/