Mohamed Lee
Mohamed Lee

Reputation: 267

Opening an app from another one to a specific VC

I'm pretty new to iOS dev and I didn't find any concrete answer.

Let's say I have an app with 2 targets, FirstApp (firstTarget) and SecondApp (secondTarget). I saw in some posts that you can open an app from another and I did do that with:

if UIApplication.shared.canOpenURL(aUrl as! URL) {
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(aUrl!)
    } else {
        // Fallback on earlier versions
    }

But can I open it to a specific VC in SecondApp?

I included UniversalLink in my project and also the AppDelegate method with an alert in it:

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
    print("UNIVERSAL LINK")
    let alert = UIAlertController(title: "Test", message:"Message", preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
    self.window?.rootViewController?.present(alert, animated: true, completion: nil)

    return true
}

The thing is, it's only working after I repeat the action 1 more time. For the first time it's only opening the app. If I go back on the FirstApp and repeat the action then the alert pops up.

Can I do it in another way or do you have a solution for this way?

Doesn't the notification work the same way? When I click on it, it redirects you to a specific VC.

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    let noNetworkView = UIView()
    var reachability:Reachability!

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        return SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
    }

    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
        let alert = UIAlertController(title: "Test", message:"Message", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
        self.window?.rootViewController?.present(alert, animated: true, completion: nil)

        return true
    }

    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        return SDKApplicationDelegate.shared.application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
    }

    func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
        return true
    }

    func applicationWillTerminate(_ application: UIApplication) {
        self.reachability.stopNotifier()
        self.saveContext()
    }

Upvotes: 1

Views: 227

Answers (1)

clayjones94
clayjones94

Reputation: 2828

continueUserActivity is not called on app launch. You will have to pull the Universal Link out of the launch options on didFinishLaunchingOptions. See this post. I recommend using Branch since they will bundle this all up into one callback for you.

Upvotes: 1

Related Questions