7

I am trying to handle open apps from universal link click. below ios 13 its working good but for ios 13 its working only app running in background. If app not working foreground or background, clicking link opens app not called continue userActivity function. I also tried to get it in scene delegate willconnnect to delegate. But still not calling My code is below what is wrong?

scene delegate

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
  guard let _ = (scene as? UIWindowScene) else { return }

  if connectionOptions.userActivities.first != nil {
    self.scene(scene, continue: userActivity!)
   }
}
    func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    continueUserActivity(userActivity: userActivity)
}
func continueUserActivity(userActivity : NSUserActivity){
    if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
        let url = userActivity.webpageURL!
        let dataDict:[String: String] = [AppLinkManager.appLinkExtraKey: url.absoluteString]

        NotificationCenter.default.post(name: .didReceiveAppLink, object: nil, userInfo: dataDict)
    }
}

app delegate

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
   if let userActivityDict = launchOptions?[.userActivityDictionary] as? [AnyHashable : Any],
      let userActivity = userActivityDict["UIApplicationLaunchOptionsUserActivityKey"] as? NSUserActivity {
       continueUserActivity(userActivity: userActivity)
   }
    return true
  } 
  func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool  {
      continueUserActivity(userActivity: userActivity)
      return true
   }


func continueUserActivity(userActivity : NSUserActivity){
    if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
        let url = userActivity.webpageURL!
        let dataDict:[String: String] = [AppLinkManager.appLinkExtraKey: url.absoluteString]

        NotificationCenter.default.post(name: .didReceiveAppLink, object: nil, userInfo: dataDict)
    }  
  }
nikinci
  • 444
  • 6
  • 25
  • Have you found any solution for this ? – Nizzam Feb 19 '20 at 05:52
  • I'm having the same issue. when the app is closed, or swiped from the recent apps and I click my dynamic link the app opens but I do not get the url information. when the app is still running in background or foreground and I click the link everything works as expected, receiving the url correctly. – Sergio Apr 02 '20 at 09:25
  • Any fix for this? – HariKarthick Aug 10 '21 at 09:36

1 Answers1

5

Have you tried implementing continue userActivity function in SceneDelegate:

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

I faced the same issue when using Firebase DynamicLink and looks like UniversalLinks (and probably several other APIs) use this callback on the SceneDelegate.

So if you're targeting iOS 13 and below try implementing both for SceneDelegate and AppDelegate

omar
  • 251
  • 3
  • 12