skip to content

When an iOS app is already running, which UIKit scene delegate method and which SwiftUI modifier receive a universal link and a custom-scheme URL?

level: middleimportance: must knowfreq 62%

answer

  1. running or suspended in memory
  2. one user activity, one URL context set
  3. webpageURL on the activity
  4. SwiftUI hands a universal link over as a URL
  5. app-delegate versions deprecated in iOS 26

basics

~10 s

In UIKit, scene(:continue:) receives a universal link as an NSUserActivity of type NSUserActivityTypeBrowsingWeb, and scene(:openURLContexts:) receives custom-scheme URLs. In SwiftUI, onOpenURL(perform:) receives both as a URL.

solid answer

~30 s

With the app running or suspended, a scene-based UIKit app gets a universal link in `scene(_:continue:)`: an `NSUserActivity` whose `activityType` is `NSUserActivityTypeBrowsingWeb` and whose `webpageURL` holds the https URL. A custom-scheme URL goes to `scene(_:openURLContexts:)` as a `Set<UIOpenURLContext>`, each with a `url` and `options` such as `sourceApplication`. Both are iOS 13 scene APIs. The app-delegate pair, `application(_:continue:restorationHandler:)` and `application(_:open:options:)`, serves only apps without scenes and is deprecated in iOS 26. In SwiftUI, `onOpenURL(perform:)` (iOS 14) receives both kinds, because SwiftUI passes a universal link straight through as a URL; `onContinueUserActivity(_:perform:)` is for other activity types such as Handoff.

code

swift · 23 lines
swift
import UIKit

final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    // Universal link while the app is running or suspended
    func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
        guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
              let url = userActivity.webpageURL else { return }
        open(url)
    }

    // Custom-scheme URL (zooapp://...) while the app is running or suspended
    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        for context in URLContexts {
            let sender = context.options.sourceApplication   // nil unless same team
            print("from \(sender ?? "unknown app")")
            open(context.url)
        }
    }

    private func open(_ url: URL) { /* validate, then route */ }
}

go deeper

for a junior

Recall the pairs: scene(:continue:) for a universal link, scene(:openURLContexts:) for a scheme URL, onOpenURL for both in SwiftUI.

for a middle

Explain the payloads: the NSUserActivityTypeBrowsingWeb check and webpageURL, UIOpenURLContext's url and options, and why sourceApplication is often nil.

for a senior

Show awareness that the app-delegate pair is deprecated in iOS 26, that adopting scenes moves delivery, and that warm and cold paths must share one handler.

for a principal

Set one entry point for every incoming link in the codebase so validation and routing rules live in a single reviewed place.

## What "already running" means here A link can reach an app in two situations. On a **cold launch** the app isn't running and the link travels with the scene's connection options. In the **warm** case covered here, the app is running or suspended in memory, and iOS brings it forward and calls a delivery method on the existing scene. Which method depends on the kind of link and on the UI framework. ## UIKit: two scene delegate methods In an app that has adopted scenes (iOS 13 and later), `UISceneDelegate` has one method per kind of link: - **`scene(_:continue:)`** receives a **universal link**. The argument is an `NSUserActivity`; check that its `activityType` equals `NSUserActivityTypeBrowsingWeb`, then read `webpageURL`, the `http` or `https` URL that was tapped. The same activity type is used for browser-to-app Handoff, so the check alone doesn't tell you which one it was. UIKit calls this method on the main thread, and only after it has received all of the activity's data. - **`scene(_:openURLContexts:)`** receives **custom-scheme URLs** such as `zooapp://tickets/8841`. The argument is a `Set<UIOpenURLContext>`; each context carries one `url` and a `UISceneOpenURLOptions` object. The options object is worth knowing: - `sourceApplication` is the bundle ID of the app that opened the URL, filled in only when that app shares your team identifier; otherwise it is `nil`. - `openInPlace` says whether a document URL can be used where it is; when it is `false`, copy the file before use. - `annotation` and `eventAttribution` carry extra data from the sender. ## Apps without a scene delegate An app that has not adopted the scene lifecycle gets the same links through the app delegate: `application(_:continue:restorationHandler:)` for universal links and `application(_:open:options:)` for scheme URLs and files. Two points matter today: 1. Both methods are **deprecated as of iOS 26**, with the SDK pointing to `scene(_:continue:)` and `scene(_:openURLContexts:)` under the scene lifecycle. 2. When a scheme URL arrives for `application(_:open:options:)` while the app is suspended or in the background, the system moves the app to the foreground before calling it; the same method also opens custom file types the app supports. Once an app has opted into scenes, Apple's documentation routes links to the scene delegate, so handlers left behind in the app delegate stop seeing them. ## SwiftUI: onOpenURL and onContinueUserActivity SwiftUI apps attach handlers to views in a scene, both available since iOS 14: - **`onOpenURL(perform:)`** registers a closure that receives a `URL`. It is called for a **universal link** and for a **custom-scheme URL** alike. Apple notes that other UI frameworks pass universal links as a user activity, but SwiftUI passes them directly as a URL. - **`onContinueUserActivity(_:perform:)`** receives `NSUserActivity` objects of the type you name, for example Handoff activities. The type string should match an entry in the `NSUserActivityTypes` array of Info.plist. Apple's documentation says to receive universal links with `onOpenURL(perform:)` instead. Which scene gets the URL depends on the app's structure, which scenes are active and their configuration; `handlesExternalEvents(matching:)` lets you steer it when an iPad app has several windows. ## Mistakes that show up in warm handlers - Handling only `scene(_:continue:)` and forgetting `scene(_:openURLContexts:)`, so the `zooapp://` link from a ticket email does nothing while the app is open. - Reading `webpageURL` without first checking `activityType`, then treating some other continued activity as a link. - Keeping the old app-delegate methods after adopting scenes and wondering why they stopped firing. - Trusting `sourceApplication` as proof of who sent a URL, when it is `nil` for every app outside your team. - In an iPad SwiftUI app with several windows, attaching `onOpenURL` only to a scene that SwiftUI doesn't route the URL to; `handlesExternalEvents(matching:)` steers it. ## Summary | Link | UIKit scene delegate (warm) | App delegate, no scenes (deprecated iOS 26) | SwiftUI | |---|---|---|---| | Universal link | `scene(_:continue:)`, `NSUserActivityTypeBrowsingWeb` | `application(_:continue:restorationHandler:)` | `onOpenURL(perform:)` | | Custom scheme | `scene(_:openURLContexts:)`, `UIOpenURLContext` | `application(_:open:options:)` | `onOpenURL(perform:)` | ## What the handler should do - Parse with `URLComponents` and reject anything malformed; Apple calls both link kinds an attack vector. - Hand the parsed result to the app's navigation code; building the screen stack is a separate job from receiving the link. - Keep the warm and cold paths calling the same function, so a link behaves the same whether or not the app was running.

  • In a SwiftUI zoo app with several iPad windows open, which window's onOpenURL receives a tapped animal link?
    Apple says the scene SwiftUI routes an incoming URL to depends on the app's structure, which scenes are active and other configuration. To control it, give scenes `handlesExternalEvents(matching:)` conditions so a matching link goes to the window meant for it rather than an arbitrary one.
  • Why might a team keep an onContinueUserActivity(NSUserActivityTypeBrowsingWeb, perform:) handler in a SwiftUI app, and why isn't it the place for universal links?
    `NSUserActivityTypeBrowsingWeb` also covers browser-to-app Handoff, so a handler for that type can serve Handoff. Apple's documentation is explicit that SwiftUI passes universal links directly as a URL and that `onOpenURL(perform:)` is the modifier to receive them, so relying on the activity handler for links misreads the API.

  • In SwiftUI, universal links arrive through onContinueUserActivity, not onOpenURL.
  • scene(_:openURLContexts:) receives universal links as well as scheme URLs.
  • A scene-based app still gets links in application(_:open:options:).
  • sourceApplication always names the app that sent the URL.
  • scene(_:continue:) runs before the activity's data has fully arrived.