Question

Here is my code :

if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
    SLComposeViewController *viewController  = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter]; //Tell him with what social platform to use it, e.g. facebook or twitter

    [viewController setInitialText:sQuotes];

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate.navController presentViewController:viewController animated:YES completion:nil];


    [viewController setCompletionHandler:^(SLComposeViewControllerResult result)
     {
         NSString *output;
         switch (result)
         {
             case SLComposeViewControllerResultCancelled:
                 output = @"Action Cancelled";
                 break;
             case SLComposeViewControllerResultDone:
             {
                 output = @"Post Successfull";

             }
                 break;
             default:
                 break;
         }

         [appDelegate.navController dismissViewControllerAnimated:YES completion:nil];
     }];
}

But it Shows the warning

on whose view is not in the window hierarchy!

Was it helpful?

Solution

The warning is pretty clear. You are trying to present a modal view by a view that is not in the window hierarchy, which won't work.

Try changing this [appDelegate.navController presentViewController:viewController animated:YES completion:nil];

to something like this instead:

[appDelegate.window.rootViewController presentViewController:viewController animated:YES completion:nil];

This guarantees that the currently active root view controller of your app will be the one presenting the modal view controller.

Depending on how your AppDelegate is built you may need to add a property or just a getter to surface the window variable to the outside world.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top