Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 Attempt to present whose view is not in the window hierarchy

Tags:

xcode

ios

swift

This question has been asked many times but even after trying most of the possible things I am still unable to find a solution that works for me. Here is the error message.

Warning: Attempt to present on <0x7f8f29e17590> whose view is not in the window hierarchy!

Note: I am not using any navigation controller.

I am just presenting a view controller modally and I have a button on it for linkedIn sign up. But every time I click the linkedin button this error appears and I am unable to see the new linkedIn dialog although it works fine in other classes.

Most solutions recommend handling button click in viewDidAppear already tried that and it doesn't work.

I am using this code for opening linkedIn signup form

linkedinHelper.authorizeSuccess({ [unowned self] (lsToken) -> Void in

        print("success lsToken: \(lsToken)")
        self.requestProfile()
        }, error: { [unowned self] (error) -> Void in

            print("Encounter error: \(error.localizedDescription)")
        }, cancel: { [unowned self] () -> Void in

            print("User Cancelled!")
    })
like image 570
Moaz Khan Avatar asked Sep 18 '17 11:09

Moaz Khan


People also ask

Is viewDidLoad only called once?

viewDidLoad() is one of the initialization methods that is called on the initial view controller. viewDidLoad() is called before anything is shown to the user - and it is called only once.

What is Viewwillappear in Swift?

Notifies the view controller that its view is about to be added to a view hierarchy.

What is a UIViewController?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.

What is child view controller in Swift?

Swift version: 5.6. View controller containment allows you to embed one view controller inside another, which can simplify and organize your code. It takes four steps: Call addChild() on your parent view controller, passing in your child. Set the child's frame to whatever you need, if you're using frames.


1 Answers

I have resolved the issue the main problem is exactly what it says the view is not in the view hierarchy. In order to resolve this issue we need to set the root view controller to the current view controller using the appDelegate object. So that the view now comes in the view hierarchy and be able to present further views. Here is the code

let initialViewController = UIStoryboard(name: "Main", bundle:nil).instantiateInitialViewController() as UIViewController
let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
appDelegate.window?.rootViewController = initialViewController

Please read this for further information. https://stackoverflow.com/a/27608804/5123516

like image 89
Moaz Khan Avatar answered Nov 14 '22 23:11

Moaz Khan