How to start using iOS Deep Link

Here you’re going to learning how can you use URL Scheme at iOS.

Arthur Rodrigues
Academy@EldoradoCPS
6 min readJun 22, 2020

--

I’m sure that someday in your life you already sent a link to someone, like your favorite song, a funny video or a publication from your social medias that you would like your friend to see. But have you ever stopped to think how does it work? How can a link take you to a particular app screen or even implement it? That’s what I want to show you today.

I’m going to teach you how you can create a deep link, share it and when you open it, send you to a particular app screen.

But keep in mind that this deep link is only for iOS and the person you are sharing must have your app to work.

So, let’s start 😄

1. Setting up info.plist

The first thing I want you to setup is the Info.plist:

  1. Add URL types
  2. Inside it, in URL identifier, you need to put the project Bundle Identifier
  3. Into Item 0, add URL Schemes and it will generate another Item 0 below. You can put whatever you want, but this scheme will be the one used as your deep link URL later on.
info.plist screenshot

Your URL types should be like the screenshot above.

2. Creating two different view controllers

Main.storyboard screenshot

I created the First View Controller, that is the initial View Controller with one share button, and the Secret View Controller, that the only way to appear will be through opening the link.

Don't forget to give a Storyboard ID for your SecretVC. I set as "secretVC".

3. Creating link

Everything is already set. So, we can generate a link that will be shared later.

I create a simple function that does this job.

1- I encoded the secret name VC as URL with query allowed because I want that this name can be sent as parameter using our URL.

2- I created the following string that will be our URL: openAppPath

3- Converting the openAppPath to URL. So, now we can share our URL.

4- We call presentActivityController that will be created on the next step.

Explaining about URLs:

  1. The scheme is used for identify the protocol to be used to access the resource on internet
  2. The host name identifies the host that holds the resource.
  3. A query string can provide an information that the resource can use for some purpose.

If you want to know more about, I recommend you to read: The components of a URL by IBM

With our deep link created, we can share the link that points to a particular content within the app, so if we want to share some video on YouTube, for example, we can copy the link and share it with someone. If we do not have a deep link, the person that you want to see the video, needs to search the video manually.

This deep link we have just created is called traditional deep link and it will work just if you have the app installed. If you do not have, it will show an error.

4. Sharing our URL

With our URL created, we can share it using the activity controller and send it as a Message.

Don't forget to call createLink in the share button IBAction

The Apple documentation says that the parameter activityItems is an array of data objects on which to perform the activity. The type of objects in the array is variable and dependent on the data your application manages. For example, the data might consist of one or more string or image objects representing the currently selected content.

Build and run your app.

If you tap the share button, it should open the activity controller.

As I didn't put any message except the URL at our share content, I can send it to someone on WhatsApp, Messenger, Email... In this case, I sent to myself as a message.

Message screenshot

Just tap on the link and the app will open.

Now your URL is working, although, how can we get to the Secret View Controller?

That's what i'm going to show you at the next step.

5. Opening at the Secret View Controller

We already learn how to create a URL scheme, how to share and now we're going to see how can we use this link to send us to a particular app screen.

In order to do this, we need to change View Controller that will appear when the application is opened by a direct user action or when it opens from the link.

The first thing you have to do is open your SceneDelegate and in the method "willConnectTo" just write the code below.

This function it will be called when the app runs and what we're doing here is:

Summing up, if we entered with a URL, this method will know, so we can check if the received URL is correct. If the parameters received at the URL is "SecretViewController" we can initialize the app with the SecretViewController.

The method that I used to query string parameter is in this little URL extension:

We’re checking if the URL is “tutorial://deep-link?appFirstScreen=SecretViewController”

If you change the URL, it will start with the FirstViewController.

Finishing:

  1. Build and Run your application
  2. Copy the URL or send it to an email or a message
  3. Close your app (if you don't close it will not open at the SecretVC, because it will not call willConnectTo method at SceneDelegate.
  4. Go to Safari, paste the URL again and go.
  5. It will show the pop up again. Just Open your app and surprise!!!

It should open the Secret View Controller 🥳

You did it!

Final result

If you want to know more about, please visit: https://medium.com/wolox/ios-deep-linking-url-scheme-vs-universal-links-50abd3802f97 that will explain more about URL Scheme vs Universal Links.

Conclusion

So, in this Medium, we learned how to create a URL Scheme that can be shared to your friends and take them to a particular screen. This could be very useful and is a nice feature to have in your app. We finally managed to share content from our apps to our friends. It’s not that difficult, is it? Tell me what you think.

GitHub repository here

Thank you for reading and if you like this article, please clap so other people can also read it 😉

Do you want to learn more about iOS? I know that you want 😅 so read my other article about Animations in iOS 🔥

Connect with me at my LinkedIn

--

--

Arthur Rodrigues
Academy@EldoradoCPS

I'm an iOS Developer and a technology enthusiast that is always looking for something to learn.