Michel
Michel

Reputation: 11755

Handling Universal Links, in the case of a non-running SwiftUI app

I am working on a SwiftUI app handling Universal Links. At this point it is pretty much working the expected way, while I am looking through the debugger at what is happening. In other words when I click the link while the app is in the background, then it wakes up and all is OK.

The magic is happening in the following function.

func scene(_ scene: UIScene, 
           continue userActivity: NSUserActivity) {
    print(#function)
    guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
    let incomingURL = userActivity.webpageURL,
    let components = NSURLComponents(url: incomingURL,
                                     resolvingAgainstBaseURL: true),
    let path = components.path else {return}
    
    let params = components.queryItems ?? [URLQueryItem]()
    
    // ... more useful code for this app ...
    .....
}

But on the other hand if I click the link when the app is just not running (not even in the background). Of course the debugger cannot be connected. Then the app fires up and starts, but the expected behavior (reacting according to the link) is not happening. Why is that?

I presume in such a case I should be dealing with the situation other than in:

func scene(_ scene: UIScene, 
           continue userActivity: NSUserActivity)
           {......}

But I am not sure.

Any relevant tip will be very much appreciated.

Upvotes: 0

Views: 712

Answers (1)

PartMen
PartMen

Reputation: 56

That flow, when the app opens, is handled by other function. You can get the URL or the URL scheme (if you are also handling that kind of links) from connectionOptions.

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
  if let urlContext = connectionOptions.urlContexts.first {
    handleDeepLinking(url: urlContext.url)
  } else if let userActivity = connectionOptions.userActivities.first, let url = userActivity.webpageURL {
    handleUniversalLinks(url: url)
  }
}

Upvotes: 1

Related Questions