Logo for tanaschita.com

Supporting universal links in a SwiftUI application

Learn how to implement universal links to be able to link to content in the iOS application and its connected website.

01 Aug 2022 · 3 min read

Universal links allow us to connect an iOS app with its related website.

When users tap a universal link, they are redirected to a specific part of an iOS application if installed. If the app is not installed, the link opens in a browser, allowing the connected website to handle it.

To support universal links, the following steps are required:

  1. Creating and configuring an associated domain file.
  2. Adding the associated domain file to the related website.
  3. Adding an associated domains entitlement to the iOS app.
  4. Update our iOS application code to receive universal links and if needed, route the user to a specific part of the app.

Let's look at these steps in more detail.

Sponsorship logo
Preparing for a technical iOS job interview
Check out my new book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, Authentication, Core Data, Concurrency with async/await, Security, Automated Testing and more.
LEARN MORE

Configuring an associated domain file

For details on how to create an associated domain file, add it to the related website and create an associated domain entitlement, check out this guide on associated domain files for iOS development.

In the article linked above, we only looked at a small example for the apple-app-site-association file. Let's see how the applinks key - which is the root object for a universal links service definition - can be configured.

{
"applinks":{
"details":[
{
"appIDs":[
"APPIDPREFIX.com.tanaschita.exampleApp"
],
"components":[
{
"/": "birds",
},
{
"?": "status",
}
]
}
]
}
}

Let's go through the example step by step:

  1. The details key is an array of objects that connects the apps with the universal links they can handle.
  2. The appIDs key is array of app identifiers with the following format: <Application Identifier Prefix>.<Bundle Identifier>. The array specifies apps that can handle universal links specified in the components array.
  3. The components key specifies patterns that define universal links an app can open.

The components object provides different patterns to match specific parts of a URL. In the example above, we used the following two patterns:

"/": "birds", // pattern to match with the URL path component
"?": "status" // pattern to match with the URL query component

Which means, that our app can handle the URL https://<domain>/birds?status=<someStatus>.

If we don't specify a path or a pattern query, the default is * which matches everything. For more configuration possibilities, check out Apple's documentation on the applinks object.

Supporting universal links in SwiftUI

SwiftUI provides the onOpenURL method which is called when a user taps on a universal link.

ContentView()
.onOpenURL { url in
// change state based on the provided URL
}

Depending on the use case, we can use the provided url to access different parts like its path, query etc. to change the state of the view, for example to route to a different screen.

Sponsorship logo
Preparing for a technical iOS job interview
Check out my new book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, Authentication, Core Data, Concurrency with async/await, Security, Automated Testing and more.
LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

associated domains

security

swiftui

ios

Implementing Password AutoFill for an iOS application

Learn how to activate Apple's Password AutoFill for SwiftUI and UIKit.

15 Aug 2022 · 3 min read

Latest articles and tips

© 2023 tanaschita.com

Privacy policy

Impressum