Universal Links Logo

Epilogue

This entry is part 12 of 12 in the series Universal Links

There is one last thing that I have implemented in a couple of my shipping apps.

In the scene delegate, I found that the scene(_: UIScene, willConnectTo: UISceneSession, options: UIScene.ConnectionOptions) method was not always being called with the urlContexts property. Instead, I had to get the URL from the userActivities property.

So, what I have done, is add a nil-coalescing (??) operator to the method, that first tries the userActivity, then fails to the urlContext.

Before:

func scene(_ inScene: UIScene, willConnectTo: UISceneSession, options inConnectionOptions: UIScene.ConnectionOptions) {
    scene(inScene, openURLContexts: inConnectionOptions.urlContexts)
}

After:

func scene(_ inScene: UIScene, willConnectTo: UISceneSession, options inConnectionOptions: UIScene.ConnectionOptions) {
    guard let url = inConnectionOptions.userActivities.first?.webpageURL ?? inConnectionOptions.urlContexts.first?.url else { return }
    
    var status: AppStatus = .off
    
    if let statusString = url.query(),
       let tempStatus = AppStatus(rawValue: statusString) {
        status = tempStatus
    }
    
    currentViewController?.updateUI(status: status)
}

I simply copied-and-pasted the code from the handler (but in my shipping apps, I have a separate common method for resolving the URLs).

Here is a release that has this fix.