David David - 6 months ago 38
Swift Question

Deeplink to ViewController in iOS app only works once after start

I'm using the following code to deep link (e.g. from Safari browser) to a specific ViewController in my app:

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool{
if(url.host == nil){
window?.rootViewController?.performSegueWithIdentifier("DeepLinkSegue", sender: nil)
print("Push-VC opened!")
print(window?.rootViewController)
print(window)
}
return true
}


When typing dlapp:// in Safari browser it should open the ViewController with the segue identifier „DeepLinkSegue“. That works fine, the ViewController opens.

But only once after launching the app. When you go to an other VC and try again to open the specific VC with „dlapp://“ just the app opens, not the specific VC. You have to restart the app otherwise it won’t work and just opens the app not the specific VC.

I first thought the function is maybe called only once. But not so because print("DL-VC opened!") always prints. Using the three print() functions in my code outputs this in console:

Push-VC opened!
Optional(<DeepLinkTest.ViewController: 0x157da1d40>)
Optional(<UIWindow: 0x157da3830; frame = (0 0; 414 736); autoresize = W+H; gestureRecognizers = <NSArray: 0x157da4a90>; layer = <UIWindowLayer: 0x157da1530>>)


You can download the project for reproduce the problem here: http://www.filedropper.com/deeplinktest_1

Regards,
David.

Answer

the problem is the segue will present vc one by one ,then you can't get rootviewcontroller to perform the segue,try to change the rootviewcontroller like this :

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool{
    if(url.host == nil){
        let sb = UIStoryboard(name: "Main", bundle: nil)
        let vc = sb.instantiateInitialViewController()
        window?.rootViewController = vc

        window?.rootViewController?.performSegueWithIdentifier("DeepLinkSegue", sender: nil)
        print("Push-VC opened!")
        print(window?.rootViewController)
        print(window)
    }
    return true
}

hope it be helpful :-)