swift Flutter IOS通用链接打开应用程序,但未导航至正确页面

0pizxfdo  于 2023-01-29  发布在  Swift
关注(0)|答案(2)|浏览(152)

我已经在我的IOS flutter项目上设置了通用链接。
正如标题所示,当我点击与我的网站相关的链接时,我的应用程序确实会打开,但它不会导航到正确的页面。它只是打开了应用程序。我没有使用uni_links包,而是使用了指南(包括官方文档)的组合:
https://developer.apple.com/videos/play/wwdc2019/717/
https://nishbhasin.medium.com/apple-universal-link-setup-in-ios-131a508b45d1
https://www.kodeco.com/6080-universal-links-make-the-connection
我已经将我的apple-app-site-association文件设置为如下所示:

{
    "applinks": {
        "details": [
            {
                "appIDs": [
                    "XXXXXXX.com.my.appBundle"
                ],
                "componenents": [
                    {
                        "/": "/*"
                    }
                ]
            }
        ]
    }
}

我把这个加到我的info.plist文件中:

<key>FlutterDeepLinkingEnabled</key>
<true/>

我的AppDelegate.swift文件看起来像这样:

import UIKit
import Flutter
import Firebase

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(_ application: UIApplication, continue userActivity: NSUserActivity, 
    restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    // This will allow us to check if we are coming from a universal link
    // and get the url with its components
    // The activity type (NSUserActivityTypeBrowsingWeb) is used
    // when continuing from a web browsing session to either
    // a web browser or a native app. Only activities of this
    // type can be continued from a web browser to a native app.
    guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
      let url = userActivity.webpageURL,
      let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else {
        return false
    }
    // Now that we have the url and its components,
    // we can use this information to present
    // appropriate content in the app
    return true
  }

  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    FirebaseApp.configure()
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

我的跑步者权利也设置正确,如:

<key>com.apple.developer.associated-domains</key>
<array>
    <string>applinks:www.example.com</string>
    <string>applinks:*.example.com</string>
</array>

问题是,如果我单击www.example.com/mypath的超链接,它不会到达/mypath处理的页面/路线,而只是打开应用程序。
我的路由是使用go_router完成的:第5.2.4章
请问有谁知道为什么会发生这种情况吗?我被这个问题挡住了。我见过类似的问题,但没有一个答案对我有效。任何帮助都是感激的。

qlvxas9a

qlvxas9a1#

好了,我明白了。苹果官方文档要求在AppDelegate.swift文件中添加这个函数的变体:

func application(_ application: UIApplication, continue userActivity: NSUserActivity, 
    restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    // This will allow us to check if we are coming from a universal link
    // and get the url with its components
    // The activity type (NSUserActivityTypeBrowsingWeb) is used
    // when continuing from a web browsing session to either
    // a web browser or a native app. Only activities of this
    // type can be continued from a web browser to a native app.
    guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
      let url = userActivity.webpageURL,
      let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else {
        return false
    }
    // Now that we have the url and its components,
    // we can use this information to present
    // appropriate content in the app
    return true
  }

看起来它与flutter框架处理通用链接的功能相冲突,把这个功能去掉,只在我的info.plist中使用这个功能就可以了(其他的都保持不变):

<key>FlutterDeepLinkingEnabled</key>
<true/>

Flutter文档并没有为此提供(在发布这个答案的时候),所以如果人们感兴趣,我可以写一篇关于必要步骤的小文章。

nwo49xxi

nwo49xxi2#

当您处理动态链接时,您将在以下函数的userActivity参数中获得通用链接和其他数据。

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    if let incomingURL = userActivity.webpageURL {
        debugPrint("incoming url is", incomingURL)
        let link = DynamicLinks.dynamicLinks().shouldHandleDynamicLink(fromCustomSchemeURL: incomingURL)
        print(link)
        let linkHandle = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { link, error in
            guard error == nil else {
                print("Error found.")
                return
            }
            if let dynamicLink = link {
                self.handleDynamicLinks(dynamicLink)
            }
        }
        if linkHandle {
            return true
        } else {
            return false
        }
    }
    return false
}

解析来自另一个函数的数据,或者你也可以解析上面的代码。在我的例子中,我解析了下面函数的代码。

func handleDynamicLinks(_ dynamicLink: DynamicLink) {
    guard let link = dynamicLink.url else {
        return
    }
    
    if let landingVC = self.window?.rootViewController as? LandingViewController {
       // Do you your handling here with any controller you want to send or anything.
    }
          // example you are getting ID, you can parse it here

    if let idString = link.valueOf("id"), let id = Int.init(idString) {
        print(id)
    }
}

当您从链接中获得详细信息时,您可以简单地获取导航控制器或VisibleController,然后可以推送到所需的流。

相关问题