Swift - pushViewController from appDelegate, rootViewController.navigationController is nil

tehfailsafe

Having a problem following a few guides, specifically http://blog.originate.com/blog/2014/04/22/deeplinking-in-ios/

I'm setting the url scheme and it's working well to launch the app from another app, but passing in the host or url are not working as it seems it should. I'm using storyboards and interface builder for all the view layouts.

The guide shows this openURL in appDelegate:

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    if([[url host] isEqualToString:@"page"]){
        if([[url path] isEqualToString:@"/page1"]){
            [self.mainController pushViewController:[[Page1ViewController alloc] init] animated:YES];
        }
    return YES;
    }
}

Here is my version simplified and in swift from a few other sources, ie Get Instance Of ViewController From AppDelegate In Swift I'm skipping the conditional for the url host at the moment to remove potential other variables in the problem.

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String, annotation: AnyObject?) -> Bool {
    var rootViewController = self.window!.rootViewController
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    var profileViewController = mainStoryboard.instantiateViewControllerWithIdentifier("profile") as ProfileViewController

    rootViewController.navigationController.popToViewController(profileViewController, animated: true)

    return true

}

The swift version causes a crash: fatal error: unexpectedly found nil while unwrapping an Optional value

It seems that rootViewController doesn't have a navigationController yet?

tehfailsafe

It seems that rootViewController actually is of type UINavigationController in my case, so casting it on declaration allowed me to call pushToViewController directly on it.

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String, annotation: AnyObject?) -> Bool {
    let rootViewController = self.window!.rootViewController as! UINavigationController
    let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let profileViewController = mainStoryboard.instantiateViewController(withIdentifier: "InstructionVC") as! InstructionVC
    rootViewController.pushViewController(profileViewController, animated: true)
    return true

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

navigationController pushViewController from UIPageViewController not work

Call navigationController from AppDelegate

Swift: navigationController returning nil

navigationController is nil in swift

self.navigationController?.pushViewController not Working Swift

navigationController!.pushViewController vs. presentViewController in iOS Swift

Instantiated NavigationController from storyboard reference the same RootViewController

navigationController is nil in Swift XCTest case

Swift - self.navigationController is nil

Swift: Can't use navigationController.pushViewController inside a function

navigationController?.pushViewController is not working

Swift PushViewController From A Different Storyboard

PushViewController from another UIViewController - Swift

Swift: How do I get access to the navigationController in AppDelegate

NavigationController.pushViewController in Custom Segue does not work - Swift 2.0 - iOS 9.1

Swift – self.navigationController becomes nil after transition

Custom Segue in Swift 2.0 : destinationViewController's navigationController property returning nil

Connect to ViewController from AppDelegate (Swift)

NavigationController wont push from function call + swift

swift segue from tableView through a navigationController to a TableViewController

CollectionView navigationController?.pushViewController() does nothing with no error

ios7 navigationController pushViewController animated bug

Accessing Objective C AppDelegate String in Swift Class returns nil

Crash when assigning rootViewController in the AppDelegate programmatically

Programmatically set rootViewcontroller for UINavigationcontroller in Storyboard in Appdelegate

How to reference current SKScene from the AppDelegate in Swift

Xcode Swift iBeacon ranging from AppDelegate

Swift: Change the tabBar icon from appDelegate

Swift: Setting ViewController background color from AppDelegate