iOS 深层 link 回调在应用程序关闭时不起作用

iOS Deep link callbacks not working when the app is closed

我在我的应用程序中使用深层链接并在我的 SceneDelegate 中使用此代码来连接到视图控制器。

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
  for context in URLContexts {
    print("url: \(context.url.absoluteURL)")
    print("scheme: \(context.url.scheme ?? "")")
    print("host: \(context.url.host ?? "")")
    print("path: \(context.url.path)")
    print("query: \(context.url.query ?? "")")
    print("components: \(context.url.pathComponents)")
  }
    window?.rootViewController?.performSegue(withIdentifier: "splashToCreateNewPassword", sender: nil)
}

当应用程序已经在后台打开时,它可以完美运行,但是当用户关闭应用程序时,它将无法运行。它只是打开应用程序的第一个屏幕。

你可以从这个代表那里得到URLfunc scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        if let userActivity = connectionOptions.userActivities.first {
            if let incomingURL = userActivity.webpageURL {
                window?.rootViewController?.performSegue(withIdentifier: "splashToCreateNewPassword", sender: nil)
            }
        }
    }

对于那些只使用 AppDelegate 的人

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        if let userActDic = launchOptions?[UIApplication.LaunchOptionsKey.userActivityDictionary] as? [String: Any],
           let userActivity  = userActDic["UIApplicationLaunchOptionsUserActivityKey"] as? NSUserActivity {
            // Do with user activity
        }
    }

我遇到了同样的问题。在 iOS(flutter build) 中,我通过添加“可用内容”解决了这个问题。文章在这里:Apple 内容可用文档。我正在使用 OneSignal,所以在 api 中我添加了该字段。现在,即使应用程序被强制关闭,它也会唤醒并且深层链接可以工作。完整的 Onesignal 邮递员代码是:

{
  "app_id": "1234",
  "included_segments": ["Test"],
  "content_available" : true,
  "contents": {
                "en": "Hi"
            },
            "data": {
                "dynamic_link": "https://google.com"
            },
            "headings": {
                "en": "Testing"
            }
}