skip to content

In a scene-based UIKit zoo app, animal links work while the app runs but land on the home screen after a cold launch; why, and how do you fix it?

level: seniorimportance: should knowfreq 45%

answer

  1. who gets called when nothing is running
  2. the scene's connection options
  3. two sets: activities and URL contexts
  4. continue is for a scene that already exists
  5. app-delegate handlers stay silent with scenes

basics

~10 s

On a cold launch, a scene-based app gets the link in scene(:willConnectTo:options:), inside connectionOptions.userActivities for a universal link or urlContexts for a scheme URL, not in scene(:continue:). Read both sets there.

solid answer

~40 s

Apple's documentation says that when a scene-based app isn't running, the system delivers a universal link to `scene(_:willConnectTo:options:)` after launch, and uses `scene(_:continue:)` only when the app is running or suspended. Custom-scheme URLs follow the same split between `connectionOptions.urlContexts` and `scene(_:openURLContexts:)`. So the usual bug is a handler that exists only in the warm methods, or a launch handler that reads only `userActivities` and misses the `zooapp://` link in `urlContexts`, or the other way round. Handlers left in the app delegate are a third cause: an app that has opted into scenes receives its links in the scene delegate. Fix it by building the window first, then reading both sets in `willConnectTo`, looking for `NSUserActivityTypeBrowsingWeb` and `webpageURL`, and sending warm and cold links to one routing function.

code

swift · 32 lines
swift
import UIKit

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

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession,
               options connectionOptions: UIScene.ConnectionOptions) {
        // ... build window and root view controller first ...
        if let activity = connectionOptions.userActivities.first(where: {
            $0.activityType == NSUserActivityTypeBrowsingWeb
        }), let url = activity.webpageURL {
            route(url)                      // universal link, cold launch
        } else if let context = connectionOptions.urlContexts.first {
            route(context.url)              // custom scheme, cold launch
        }
    }

    func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
        guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
              let url = userActivity.webpageURL else { return }
        route(url)                          // universal link, warm
    }

    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        guard let url = URLContexts.first?.url else { return }
        route(url)                          // custom scheme, warm
    }

    private func route(_ url: URL) {
        // validate, then hand to the app's router
    }
}

go deeper

for a junior

Recall that a link which launches the app arrives in scene(_:willConnectTo:options:), not in the methods used while the app is running.

for a middle

Explain the two connection-option sets: userActivities with an NSUserActivityTypeBrowsingWeb activity, urlContexts with UIOpenURLContext values.

for a senior

Diagnose the lost link from symptoms: warm-only handlers, one set read, app-delegate leftovers after adopting scenes, routing before the window exists; then test cold deliberately.

for a principal

Require a single link entry point and cold-launch link tests in the release checklist so every new link type covers both paths.

## What happens on a cold launch A **cold launch** here means the zoo app isn't in memory when someone taps a link. iOS launches it, creates a `UIWindowScene` and calls the scene delegate's **`scene(_:willConnectTo:options:)`**. Any reason the scene was created, such as a tapped link, travels in the third argument, a `UIScene.ConnectionOptions` object that UIKit fills and that you never create yourself. Apple's documentation draws the line clearly for both kinds of link: - A **universal link** with the app not running goes to `scene(_:willConnectTo:options:)` after launch; tapped while the app is running or suspended in memory, it goes to `scene(_:continue:)`. - A **custom-scheme URL** with the app not running also goes to `scene(_:willConnectTo:options:)`; while running or suspended, it goes to `scene(_:openURLContexts:)`. Apple documents the warm methods for links that arrive while the app is running or suspended, not for the link that launched it. A handler that lives only there misses that link, and the app shows its default home screen. ## Where the link sits in the connection options | Link kind | Connection options property | What to read | |---|---|---| | Universal link | `userActivities`, a `Set<NSUserActivity>` | the activity whose `activityType` is `NSUserActivityTypeBrowsingWeb`, then `webpageURL` | | Custom scheme | `urlContexts`, a `Set<UIOpenURLContext>` | each context's `url` and `options` | Both are sets, and an empty `urlContexts` simply means there are no URLs to open. The same object also carries unrelated launch reasons, such as a quick action's `shortcutItem` or a notification response. ## How the link gets lost - **Only the warm methods are implemented.** `scene(_:continue:)` and `scene(_:openURLContexts:)` work in testing because the developer's app is usually already running; the cold path was never written. - **Only one entry point is read.** The launch code checks `userActivities` for universal links but forgets `urlContexts`, so the `zooapp://` fallback in a ticket email is lost; or the reverse. - **The handler sits in the app delegate.** `application(_:continue:restorationHandler:)` and `application(_:open:options:)` belong to apps without scenes. Once the app has opted into scenes, links go to the scene delegate, and since iOS 26 those app-delegate methods are deprecated anyway. - **The link is read before there is anything to show it in.** Code that parses the link and immediately tries to push a screen before the window and root view controller exist has nowhere to put it. - **The activity type isn't checked.** Taking `userActivities.first` without checking `NSUserActivityTypeBrowsingWeb` can pick up an activity that isn't the link. ## The fix 1. In `scene(_:willConnectTo:options:)`, create the window and root view controller first. 2. Look in `connectionOptions.userActivities` for an `NSUserActivityTypeBrowsingWeb` activity and read its `webpageURL`. 3. Otherwise, read `connectionOptions.urlContexts` for a scheme URL. 4. Send whichever URL you found to the **same** routing function that `scene(_:continue:)` and `scene(_:openURLContexts:)` call, so cold and warm links behave the same. 5. Validate the URL there before acting on it, as Apple advises for both kinds of link. ## Why the two paths diverge A running scene already has a window, a view hierarchy and a navigation state, so UIKit can hand it the link as a separate event. A launching scene has none of that yet; the link is one of the reasons the scene exists at all, so UIKit packs it into the connection options together with the other launch reasons. That is why the same link needs two code paths in UIKit, and why both paths should end in one function: the parsing, the validation and the decision about which screen to show must not depend on whether the app happened to be in memory. ## SwiftUI apps An app built on the SwiftUI life cycle usually doesn't write these scene delegate methods. It registers `onOpenURL(perform:)`, which SwiftUI calls with a universal link or a custom-scheme URL as a plain `URL`; the same rule applies there, one handler for every link. ## Testing the cold path Force-quit the app, then tap the link from another app such as Notes or Messages, for a universal link and for the scheme link alike. Repeat with the app suspended in the background. Both runs should reach the same screen; which screens sit beneath it in the back stack is a navigation design question beyond delivery.

  • Why does the bug rarely show up while developing the zoo app from Xcode?
    During development the app is usually running or suspended when a link is tapped, so iOS calls `scene(_:continue:)` or `scene(_:openURLContexts:)`, which work. The cold path through `scene(_:willConnectTo:options:)` only runs when the app isn't in memory, so it must be tested deliberately by force-quitting the app first.
  • The launch code calls route(url) before setting window.rootViewController; what goes wrong, and what is a safe pattern?
    The router has no navigation controller or root view to update yet, so the push silently does nothing and the home screen appears. Build the window and root first, then route; or store the URL as a pending link that the root consumes once it is on screen.

  • iOS calls scene(_:continue:) right after willConnectTo for the launching link.
  • Universal links on a cold launch arrive in connectionOptions.urlContexts.
  • Keeping application(_:continue:restorationHandler:) covers scene-based apps too.
  • Testing links with the app already running proves the launch path works.
  • Any activity in userActivities can be treated as the tapped link.