Trying to imagine ... whose view is not in the hierarchy of windows

I developed using WinDev Mobile, which generates a project that I opened with Xcode for compilation. Only the WM sharing features on Facebook are not yet developed, I have to develop in ObjC

I started with the code:

if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {

    SLComposeViewController *controllerSLC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook] ;
    [controllerSLC setInitialText:myMessage];
    [controllerSLC addURL:[NSURL URLWithString:myUrl]];
    [controllerSLC addImage:[UIImage imageNamed:myImage]];

    // NSLog (@"% @", [UIApplication sharedApplication] keyWindow.rootViewController.)

    [self presentViewController:controllerSLC animated:YES completion:Nil];

    controllerSLC.completionHandler = ^(SLComposeViewControllerResult result) {
        NSString *output = nil;
        switch(result) {
            SLComposeViewControllerResultDone box:
                output = @ "Your tweet has-been sent" ;
                status = 2;
                // NSLog (@" SENT ");
                break;
            SLComposeViewControllerResultCancelled box:
                output = @"Your tweet has-been canceled ";
                status = 3;
                // NSLog (@" canceled ");
                break;
            default:
                break;
        }
        [controllerSLC dismissViewControllerAnimated:YES completion:nil] ;
    };

}

I got an error:

Warning Attempt to present <SLComposeViewController > on <CFenPrincipaleViewController> Whose view is not in the window hierarchy !

I added the code:

UIViewController *activeController = [UIApplication sharedApplication] keyWindow.rootViewController. ;
if ([ activeController isKindOfClass:[UINavigationController class]]) {
    activeController = [(UINavigationController *)activeController visibleViewController] ;
}
[activeController presentViewController:controllerSLC animated:YES completion:Nil];

It works on the home window application, I have no error.

But when I call the same function in another window, the error returns .... Thank you in advance for your help.

+4
source share
1 answer

I had the same problem ... give this chance!

First get the root view controller ....

-(UIViewController*) getRootViewController {
return [UIApplication sharedApplication].keyWindow.rootViewController;
}

Now instead activeController = [(UINavigationController *)activeController visibleViewController] ;

Do it:

UIViewController* activeController = [self getRootViewController];

[activeController presentViewController:controllerSLC animated:YES completion:Nil]; .

, .

+4

Source: https://habr.com/ru/post/1536936/


All Articles