Universal Links: URL Patterns

Karthik
3 min readOct 13, 2021

A software design pattern is a solution to a particular problem you might face when designing an app’s architecture. Let’s take a look at the URLPatterns framework for dynamic links pattern matching.

Framework Source:
https://github.com/johnpatrickmorgan/URLPatterns

Universal Links

Universal links are Apple’s way of launching apps in iOS. They link to content inside an app or website, giving iOS users an integrated mobile experience.

Broadly, there aren’t many differences between universal links and traditional deep links. However, universal links are a technology specifically created for Apple devices to open a web page from an application.

Universal Links are standard web links (http://mydomain.com) that point to both a web page and a piece of content inside an app. On tap of Universal Link, iOS checks to see if any installed app is registered for that domain. If so, the app is launched immediately without ever loading the web page. If not, the web URL (which can be a simple redirect to the App Store) is loaded in Safari.

AASA (apple-app-site-association) File

The AASA (apple-app-site-association) is a file that lives on your website and associates your website domain with your native app. It contains the app ID of each app that should be associated with that domain, as well as, all of the paths that each of those apps should handle. With URI schemes, app developers could register any URL scheme of their linking without any verification and would respond to those URL schemes by opening apps. The AASA file helps us to prove the domain ownership to iOS by restricting others from hosting an AASA file on your website domain.

Example: I have added facebook’s AASA file for your reference to view app links that invoke various processes.
https://facebook.com/apple-app-site-association

Note: The file should not have an extension and its size must not exceed 128 KB.

Sample AASA json from Facebook

#1 Pattern Matching with Contains

I have taken a sample intern profile applink from the above file, to extract the userId of the profile. Using the UserId I can navigate and view that particular intern profile.

https://facebook.com/intern/profile/{id}

I used the above URL to verify whether it contains an intern/profile in it. If so, the last element of the url path component contains the userId of that profile. Since the “Contains” query considers all the items during iteration, it takes O(n) time to complete the process.

Imagine, if we want to fetch multiple records. The above pattern works for retrieving a single record but not multiple records

https://facebook.com/intern/profile/{id}/followers/{id}/profile

Since it is so hard to fetch multiple records using a simple “Contain “ query, we decided to shift to an alternate pattern.s

#2 Pattern Matching with URLPatterns Framework

While I was researching logics and libraries to solve the i above pattern matching, I found out that URLPatterns libraries are the best solution for our requirement.

Logic: The logic used behind URLPatterns lib pattern matching are:

  • Path components are extracted from the given url
  • Defining Counted enum to pattern match paths with any number of components.
  • Using Enum with Associated type to extract various ids for consumption.

Wrapping it up!

Your direction is more important as much as your speed.

URLPatterns framework is easy to read, implement and extract the details out of the URL with O(1) time.

Having a design pattern in your skillset will help you to build fully functional and secure applications that are easy to maintain and upgrade.

--

--