Search code examples
iosswiftdeep-linkingurl-scheme

Is it possible to move from one ViewController to another by using URLScheme?


I'm working on a project that user can opens the app from outside and showing ProductViewController (a ViewController that shows the specific product with some info like name, mode, price and ...) for this part, I using this func inside AppDelegate:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool 

I wanted to put some shortcuts button inside app, that when the user tapping on that button, can reach to the same product in ProductViewController too.(imagine after one time a user by a product I put some shortcut so the user can find that product fast) I know I can do this by using this code:

let productVC = ProductViewController()
present(productVC, animated: true)

but I want to know can I use that part of codes(inside AppDelegate for doing this too). I did't want to use the both ways to doing one job.


Solution

  • Yes, you can have a button inside your app call openURL (or rather application(_:open:options)) with a URL that invokes a private URL scheme. You'd then have your app delegate decode the URL, figure out what view controller the URL specifies, and open that view controller.

    Just have the IBAction for your button invoke application(_:open:options) with your custom URL. You could use something like:

    myApp://open?VC=ProductViewController

    Where open is a verb your app looks for to tell it to open a view controller, and the VC query parameter tells it which VC to open.

    What is it you want help with, exactly?