0

For my view controller's viewDidLoad method implementation, it asks the user for address book access. I need to make it so that if the user taps the "Don't allow" button on the alert view window, then we will segue them to another view controller.

My problem is that the segue never happens and shows this output in the console:

Warning: Attempt to present <MediaCaptureVC: 0x156788c0> on <AddFriendsViewController: 0x15670dd0> whose view is not in the window hierarchy!

I have tried several things that have not helped:

  1. Adding a viewDidAppear method implementation with my segue code inside of it and calling [self viewDidAppear] in my viewDidLoad's if statement.

  2. Moving my segue code to the very bottom of viewDidLoad.

But nothing works. I need a way to trigger the segue even if the view technically is loading because if they deny access to their address book then there is absolutely no reason for them to stay on the view controller.

Here is the beginning of my viewDidLoad method implementation where I ask for address book access, and then my if statement of if(accessGranted == NO) contains my segue code of [self performSegueWithIdentifier:@"addFriendsToMediaCaptureSegue" sender:self];

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view.

[self.navigationController setNavigationBarHidden:YES];

self.currentUser = [PFUser currentUser];



self.tableView.dataSource = self;
self.tableView.delegate = self;


//Asks for access to Address Book.

ABAddressBookRef m_addressbook =  ABAddressBookCreateWithOptions(NULL, NULL);



ABAddressBookCopyArrayOfAllPeople(m_addressbook);


__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        @autoreleasepool {
            // Write your code here...
            // Fetch data from SQLite DB
        }
    });

    ABAddressBookRequestAccessWithCompletion(m_addressbook, ^(bool granted, CFErrorRef error)

    {
        accessGranted = granted;

        NSLog(@"Has access been granted?: %hhd", accessGranted);


        if(accessGranted == NO) {

            [self performSegueWithIdentifier:@"addFriendsToMediaCaptureSegue" sender:self];


        }

        NSLog(@"Has there been an error? %@", error);


        dispatch_semaphore_signal(sema);
    });
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
user3344977
  • 3,584
  • 4
  • 32
  • 88

2 Answers2

0

In your viewDidLoad:

double delayInSeconds = 0.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    SecondViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
    [self presentViewController:vc animated:YES completion:nil];
});;
Himanshu Joshi
  • 3,391
  • 1
  • 19
  • 32
0
  1. Be sure to have (in your storyBoard), a defined segue from current ViewController to destinationVC named addFriendsToMediaCaptureSegue.
  2. Can you try to move your performSegueWithIdentifier: like this

    dispatch_async(dispatch_get_main_queue(), ^{ [self performSegueWithIdentifier:@"addFriendsToMediaCaptureSegue" sender:self]; });

Armand DOHM
  • 1,121
  • 1
  • 7
  • 9
  • 1
    Can you try to add a button in your VC, and perform the segue on click of this button ? Just to see if your bug is related to where is the code. – Armand DOHM Mar 13 '14 at 09:47