Deep Linking on iOS: How URL Schemes and Universal Links Open Your App, With SwiftUI and UIKit Setup

Deep linking on iOS is opening a specific screen inside an app from a URL, either a custom URL scheme the app registers or a Universal Link, a standard https link that the app's domain verifies (Apple Developer, Defining a custom URL scheme for your app and Allowing apps and websites to link to your content). The screen is inside a mobile app, not a page deep inside a website.

The main benefit is that the person lands on the content the link promised, one product, one plan or one invite, instead of the app's home screen. Universal Links work from iOS 9, and SwiftUI's onOpenURL handler from iOS 14 (Apple, applinks and onOpenURL(perform:) references).

iOS deep linking runs on two kinds of URL. URL schemes like myapp:// are quick to register and claimed by whichever app registers them. Universal Links are https links that open the app only when the domain's file lists it. Both reach the app through the same few methods, and the SwiftUI and UIKit setup below covers each, followed by testing and the common failures of deep links on iOS. For deep linking in iOS without the app installed, the last sections show what changes.

How does deep linking work on iOS?

Deep linking works on iOS by letting the system, not the browser, decide which installed app gets a tapped URL, in four steps:

  1. The person taps a URL in Mail, Messages, an ad or another app.

  2. iOS checks whether an installed app registered the URL's scheme or, for an https URL, claims its domain in Associated Domains with a matching file on the site.

  3. iOS launches or foregrounds that app and hands it the URL.

  4. The app reads the path and query and opens the screen.

When no app claims the URL, a Universal Link opens in the default browser, so the website handles it, while a custom scheme opens nothing (Apple, Allowing apps and websites to link to your content). That difference in step 2 is why the two kinds of link behave so differently.

Custom URL schemes and Universal Links differ in who is able to claim them and what happens without the app:

Attribute

Custom URL scheme

Universal Link

Looks like

myphotoapp:albumname?index=1

https://example.com/albums?index=1

Who claims it

Any app that registers the scheme; with two, "the app the system targets is undefined"

Only apps the domain's file lists

Verified by

Nothing

The apple-app-site-association file on the domain

Without the app

Nothing opens

The page opens in the browser

Apple's advice

"an acceptable form of deep linking"

"strongly recommended"

The quotes are from Apple's page on custom URL schemes. On the question of a deep link vs universal link on iOS, a Universal Link is a deep link with a verified https address, and every link shared outside the app should be one. The guide to Universal Links compares both with Android App Links. A scheme still has uses inside your own apps and tools, so its setup comes first.

How do you register a custom URL scheme?

You register a custom URL scheme in the target's Info tab, under URL Types:

  1. Open the app target's Info tab in Xcode.

  2. Add a URL Type.

  3. Enter the scheme in URL Schemes, without ://.

  4. Choose the Editor role for a scheme you define, or Viewer for one you adopt.

  5. Give it a reverse DNS identifier, such as com.example.myapp.

The identifier is a convention, not a lock: Apple says reverse DNS "doesn't prevent other apps from registering the same scheme and handling the associated links." The system delivers a scheme URL to the app delegate's application(_:open:options:). An app that uses scenes gets it in scene(_:willConnectTo:options:) at launch, through connectionOptions.urlContexts, and in scene(_:openURLContexts:) while it runs (Apple, Defining a custom URL scheme for your app). Keep schemes for links between your own apps and for debugging; links people share need a domain behind them.

You set up Universal Links for an iOS app by adding the Associated Domains entitlement for each domain and serving a file on each domain that lists the app, in four steps:

  1. Add the Associated Domains capability under Signing and Capabilities in Xcode.

  2. Add one applinks:<domain> entry per domain, such as applinks:example.com, with no scheme, path, query, trailing slash or port.

  3. Host the apple-app-site-association file on each of those domains, listing the app's ID and the paths it opens.

  4. Install a fresh build on a device, so the system fetches the file for each domain.

Apple's rules for step 2 are strict: "Don't include path and query components or a trailing slash", *.example.com covers every subdomain, and www.example.com and example.com are two entries, each with its own file (Apple, Supporting associated domains). TN3155 adds that "applinks:example.com:8080 will not open your app." With automatic signing, Xcode updates the App ID and the provisioning profile for you (Apple, Configuring an associated domain); with manual profiles, enable the capability on the App ID in the developer account, or the entitlement is signed as disabled (ulink.ly). The pieces are the same whatever builds the link, and Smler's page on deep linking shows them for a short link domain. Once iOS trusts the domain, the app has to receive the link.

A SwiftUI app receives the link through .onOpenURL on a view in the scene, which SwiftUI calls with a URL for both a custom scheme and a Universal Link, on iOS 14 and later (Apple, onOpenURL(perform:)).

Swiftyn's guide, which ranks for this query, tells SwiftUI readers to catch Universal Links with onContinueUserActivity(NSUserActivityTypeBrowsingWeb). Apple's reference says otherwise: "SwiftUI passes a Universal Link to your app directly as a URL, which you receive using this modifier." onContinueUserActivity is for other activities, such as Handoff. One handler therefore covers both kinds of link:

Code
import SwiftUI

enum Route: Hashable {
    case product(id: String)
    case invite(code: String)
}

@main
struct ShopApp: App {
    @State private var path: [Route] = []

    var body: some Scene {
        WindowGroup {
            NavigationStack(path: $path) {
                HomeView()
                    .navigationDestination(for: Route.self) { route in
                        switch route {
                        case .product(let id): ProductView(id: id)
                        case .invite(let code): InviteView(code: code)
                        }
                    }
            }
            .onOpenURL { url in
                if let route = Route(url: url) {
                    path = [route]
                }
            }
        }
    }
}

The Route(url:) initializer is where parsing lives, as the routing section below shows. To open another app's link from SwiftUI, use the openURL environment value. UIKit apps get the same URL through older methods, one per app state.

Which UIKit methods deliver the URL?

UIKit delivers the URL through a different method for each kind of link and each app state:

Link and app state

Method that gets it

Universal Link, app delegate only

application(_:continue:restorationHandler:), with an NSUserActivity of type NSUserActivityTypeBrowsingWeb and the link in webpageURL

Universal Link, scenes, app not running

scene(_:willConnectTo:options:), in connectionOptions.userActivities

Universal Link, scenes, app running or suspended

scene(_:continue:)

Custom scheme, app delegate only

application(_:open:options:)

Custom scheme, scenes

connectionOptions.urlContexts at launch, scene(_:openURLContexts:) while running

The table follows Apple's Supporting universal links in your app and its custom scheme page. A scene delegate handles a Universal Link tapped while the app runs like this:

Code
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
          let url = userActivity.webpageURL else { return }
    Router.shared.open(url)
}

Implement both scene methods: an app that reads only one opens the right screen in one state and the home screen in the other. Whichever method receives the URL, it should hand it to one router.

How do you route the URL to the right screen?

You route the URL to the right screen by sending every link through one parser that turns it into a route:

  1. Send every URL, scheme or Universal Link, from every method above to one router.

  2. Parse it with URLComponents, never by splitting strings.

  3. Map the path to a route value, such as /products/42 to .product(id: "42").

  4. Load the screen's data from the ID in the URL, not from the screen before it.

  5. Send any URL the router doesn't know to the home screen.

Code
extension Route {
    init?(url: URL) {
        guard let parts = URLComponents(url: url, resolvingAgainstBaseURL: true) else { return nil }
        let segments = parts.path.split(separator: "/").map(String.init)
        switch segments.first {
        case "products" where segments.count == 2:
            self = .product(id: segments[1])
        case "invite":
            guard let code = parts.queryItems?.first(where: { $0.name == "code" })?.value,
                  !code.isEmpty else { return nil }
            self = .invite(code: code)
        default:
            return nil
        }
    }
}

Treat every link as input from a stranger: Apple warns that links "offer a potential attack vector into your app", so validate each parameter, drop malformed URLs, and never let a link delete content or reach sensitive data. Branch's tutorial makes the same point about data: a screen that relies on "stable identifiers instead of upstream state" survives being opened first. This pattern is the core of deep linking iOS Swift code; the next step is proving it works.

You test iOS deep links in four steps, from the simulator to a real device:

  1. Open the URL in the booted simulator with xcrun simctl openurl booted "https://example.com/products/42", the command Smler's deep links introduction gives.

  2. On a device, paste the link in Notes and long-press it; when both the app and the browser are offered, the Universal Link works.

  3. Turn on Developer Mode, then under Settings, Developer, Universal Links, turn on Associated Domains Development, open Diagnostics and enter the full URL.

  4. Tap the link from Mail or Messages, never from Safari's address bar.

TN3155 explains the last two rules: the choice made on a long-press "will set the default behavior for your device when following universal links from this domain in the future", and "Entering the URL directly into the web browser's address bar will never open the app" (Apple, TN3155 Debugging universal links). Use the simulator to check routing, and confirm Universal Links on a device before release, since the TN3155 tests run on one. The guide on how to test deep links on iOS covers the full set. A link that passes on Android and fails here usually fails for an iOS reason.

A deep link fails on iOS but works on Android when something only iOS checks is wrong: the file, the entitlement, or where and how the link was opened. When deep linking is not working on iOS, look for one of five causes:

  • No file: the domain or subdomain has no apple-app-site-association file of its own.

  • Entitlement: Associated Domains misses the domain, or the entry carries a path or a port.

  • Same domain: the link was tapped on a page of the same domain in Safari, which keeps it in the browser.

  • Opened by the app: the app opened its own Universal Link with open(_:options:completionHandler:) or SwiftUI's openURL.

  • User choice: the person once chose the browser for that domain.

Apple states the third and fourth causes directly: "When a user browses your website in Safari and taps a universal link in the same domain, the system opens that link in Safari", and "If your app uses one of the above methods to open a universal link to your website, the link won't open in your app." Check the file and the entitlement first, since each is set once and breaks every link on the domain. The guide to universal links not working tests for all nine causes. One case no fix covers: the person doesn't have the app yet.

iOS passes nothing from a tapped link to an app installed afterward, so a deferred deep link needs a service that records the click and matches the app's first open to it. Without one, a Universal Link opens the website and a custom scheme opens nothing.

Smler's docs describe two ways to make the match on iOS. Clipboard matching passes the link through the App Store install. Probabilistic matching is the fallback when the clipboard gives nothing. The mechanics are on the deferred deep linking page, and the Swift setup is in the guide to deferred deep linking on iOS. The route that opens the screen is the same router built above, fed by the service instead of the system.

Where does Smler fit in iOS deep linking?

Smler fits around the four steps of the first section: it serves the domain's file that step 2 checks, and it carries the link through the install when step 3 has no app to hand it to. Smler supports https links only, so on iOS it uses Universal Links, with no custom URL schemes. It hosts the apple-app-site-association file on the link's domain once the domain is set up in Smler. To mark the first open after install as converted, the app or its backend sends POST /api/v2/track/:clickId, and the default attribution window is 90 days, configurable. Smler's pricing meters plans by attributed installs, with 25,000 a month on the Free plan and $19 a month for the first paid plan.