Solve controller routing problem by using a URL-scheme

Maxud Daudov
iOS App Development
3 min readApr 2, 2018

At some point every team gives up on using storyboards. So did we at Applicatura. So, we have to configure all routings and layout programmatically. It’s OK, if you have screens that have one-two links to them. But what if your screen can be shown, literally, from every place in your application. How to do that….

First lets see whats the problem about, and one by one move toward the solution.

Whats the problem

Let’s figure out the problem, using a specific example. For instance, we’re going to build an internet-shop app with product card, listings, catalogues, discoveries and offers(and lot of more), or social app with profiles, list of friends, chats and so on. Definitely, we can notice that it’s necessary to show a product card or user’s profile from many controllers and it would be great to implement this logic once and reuse it. DRY, we remember you! We can’t achieve it by using storyboards, you may imagine how it’d look within a storyboard — weeeeb. 😬

So, you can remember VIPER with its R(Router). Why doesn’t it solve the problem? Well, you will need to solve the problem in every router, and imagine you change API of product card controller. You will need to change it everywhere! Hope you don’t like it, so don’t we(Otherewise you should meet your psychologist).

You could use MVVM+Router approach, which is good as well, but it didn’t fit our requerements. So…

URL Scheme

URL scheme is url that exactly identifies some place in your application with exact content. You should have seen this before. Many application adapt url-schemes for inter-applicaiton communication.

Here is Instagram url-sheme:

`instagram://media?id=MEDIA_ID`

Seem familiar, ha? If it’s not, don’t worry. This will be clear soon.

Lets see what we have

  1. ‘instagram://’ — identifies application on the device. Actualy you could have many applications with this scheme, but OS will open the most recent one.
  2. ‘media’ — location in the application. Here we tell, that we want to see some media
  3. ‘id=MEDIA_ID’ — parameter — value. Identifies the media content, we want to see.
That’s what we gain, using this approach

So, why can’t we use something like that to route in out application?

Yes We Can!

We can gain similar behaviour, by generating URL scheme and resolving that.

First, we need some container, so store our handlers

Here it is. We will save in ```deeplinkHandlers``` our schemes and handlers for them. Handler can open it, pass some data, close or anything else.

Next, lets feel up our container:

Here, we initialize our Router with some deep links(that’s how we call url-schemes). We can pass to Router some url, and our show handler will present view associated with this URL.

How to send some information to controller, you could ask? And here wemove forward.

We need some method to parse the url:

Here we can get url parameters(like product id or something). In `lastParsedUrl` we save url, for processing in controller we want to present.

Thats it!!! Congratulations, your base for routing is ready. How to use it?

One thing you should remember. IT’S URL-SCHEME, so don’t forget add scheme to your URL(it’s ‘instagram://’ part).

We should always remember: architecture is meant to help us make systems cleaner, more testable and predictable. Don’t trouble yourself, using this pattern everywhere, even when using standard approach whould be better. Make the right choice. At the end, you give it a new life.

Hope, you enjoyed this post, and will be glad for your response.

Good luck;)

--

--