5

As per my App project setup, I have following function calls with same code to instantiate rootVCs in SceneDelegate and AppDelegate respectively

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

}

func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?)
        -> Bool {
}




In order to Implement Universal Links, I have the following callback function in my App Delegate



func application(_ application: UIApplication,
                     continue userActivity: NSUserActivity,
                     restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
//code to capture and setup universal link
}


This function from AppDelegate is only called in less than iOS 13 devices.I looked for similar callback equivalent for SceneDelegate, The closest I could find was this function.



func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
//code to capture and setup universal link
}

Configuration: Xcode Version 11.5 target iOS 10+ devices. 
Problem: This particular callback is only called when there is an instance of the app running before Link is clicked. i.e. Once the App instance is killed, this function from SceneDelegate is not called and universal links Do not work for iOS13+ Devices.

I tried following this Xcode 11 - Opt out of UISceneDelegate/SwiftUI on iOS 13 to remove the Scene Delegate altogether, However ended up with only Black Screen. Question: What am I doing wrong and what is the possible fix?



Sameeh Ahmed
  • 73
  • 1
  • 11
  • Still trying to debug this, but I believe you need to implement: ```swift func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { ``` – David Jul 17 '20 at 00:35
  • Yes, I have implemented that (as seen in code snippet above). I couldn't find any solution to this. worked around by Removing Scene Delegate from my project completely to get Callback work right. – Sameeh Ahmed Jul 17 '20 at 11:35
  • This answer may be help you to get the call back: https://stackoverflow.com/a/67592985/14046295 – SomuYadav Jan 28 '23 at 21:56

1 Answers1

0

I had the same problem, the problem is that you have a SceneDelegate, because of this the AppDelegate methods are not called. So you are missing one method in your SceneDelegate, the first one you left empty will handle Universal links when the app has not been launched yet.

Implement the following methods in your SceneDelegate to handle Universal Links when the app is already running and when it is not launched yet:

//This method is called when app is NOT running in the background.
//Easy to test with fatalError()
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let _ = (scene as? UIWindowScene) else { return }
    
    if let userActivity = connectionOptions.userActivities.first {
        debugPrint("userActivity: \(userActivity.webpageURL)")
        fatalError()
    }
}

//This method is called when app is running in the background.
//Easy to test with debugPrint
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    
    debugPrint("userActivity: \(userActivity.webpageURL)")
}

From there, do whatever you need to handle the links.

Hope it helps :)

Mikkel Cortnum
  • 481
  • 4
  • 11