0

I'm incredibly confused... When a user clicks a button on my main page, if we have access to their contacts we push a table view controller onto our nav controller. If not, we ask for access, and if granted we push the same table view controller. (currently the tableviewcontroller is just a filler). Here is the weird thing, if we ask them for access to their contacts, when we push the table view controller it doesn't show up. it just stays on the current page, even though if i check the logs it says the view controller was added to the nav controller.

The code checking access to their contacts:

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

}

And the code for '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]);
}

Note: even when it fails the log still displays:

  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>"
)

However, I do have a check in ContactViewController:viewDidLoad and this does not fire on failures, so the load isn't being done for some reason?

Thank you

pasquers
  • 772
  • 1
  • 8
  • 24

1 Answers1

1

You need to make sure your UI code is called on the main thread:

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];
    });
});
rmaddy
  • 314,917
  • 42
  • 532
  • 579