Hi friends , Good to see you after a long time.A belated Happy New Year to all of You.  Today we will learn about deep linking in iOS. Using deep linking we can route to our app from safari browser.

Steps:

  1. Create a new Xcode Project Lets say DeepLinking.
  2. Now create a new class called secondViewController.swift and take a ViewController   in StoryBoard and change its class to secondViewController.
  3.  Now go to plist and define URLSchema for the app which we will use to redirect to this app.

screen-shot-2017-01-22-at-12-49-05-pm

  1. Now go to AppDelegate and write below method which will  handle the url when triggered from safari browser.

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

          let urlPath : String = url.path as String!

        let urlHost : String = url.host as String!

        let mainStoryboard: UIStoryboard = UIStoryboard(name: “Main”, bundle: nil)

        if(urlHost != “iOStpointBlog.com”)

        {

            print(“Host is not correct”)

            return false

        }

        if(urlPath == “/second”){

         let secondVC: secondViewController = mainStoryboard.instantiateViewController(withIdentifier: “secondViewController”) as! secondViewController

            self.window?.rootViewController = secondVC

        }

        self.window?.makeKeyAndVisible()

        return true

    }

5. Now run the app on simulator and then stop it.

6. Go to safari broswer and hit the below URL.

iOStpointBlog://iOStpointBlog.com/second

You can download the whole project from here.

Do share your reviews and comments.

Enjoy Coding 🙂

Advertisements