8

我一直在努力在我的应用中包含动态链接。我已对其进行了设置,以便可以正确生成和正确接收链接,但它们仅在应用程序打开并在后台运行时才有效。如果应用程序完全关闭,链接只会打开应用程序。即使我在 didFinishLaunchingWithOptions 方法中返回 TRUE,它看起来也没有调用 AppDelegate 中的 continueUserActivity 方法。我已经读到我应该在 didFinishLaunchingWithOptions 方法中获取传入的 URL,但是可用的答案不起作用并且已经过时,所以我希望有人会知道。

这是代码:

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate{

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    FirebaseApp.configure()
    return true
}

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
            
    if let incomingURL = userActivity.webpageURL {
        
        let linkHandled = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamicLink, error) in
            guard error == nil else {
                print(error!.localizedDescription)
                return
            }
            if let dynamicLink = dynamicLink {
                self.handleIncomingDynamicLink(dynamicLink)
            } else {
                print("Something went wrong")
            }
        }
        if linkHandled {
            return true
        } else {
            return false
        }
    }
    return false
}

func handleIncomingDynamicLink(_ dynamicLink: DynamicLink) {
    guard let url = dynamicLink.url else {
        print("No incoming link")
        return
    }
    print("Link is: \(url)")
    SystemManager.sharedInstance.setDeepLinkTrip(tripKey: deepLinkParser(link: url))
    
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let initialViewController = storyboard.instantiateViewController(withIdentifier: "RootVC")
    self.window?.rootViewController = initialViewController
}

那么,这些天来,当应用程序冷启动时,如何才能正确打开链接呢?感谢所有帮助:)

编辑:我已经更新了我的应用程序以使用 SceneDelegate,这是所有其他答案的建议,但它仍然不起作用。如果在后台运行,它仍然会打开动态链接,否则不会打开它们。看起来没有任何链接被发送到应用程序。我找不到任何 userActivity、urlContexts 等。

继承人新的相关代码:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?

//I read that this function should be handling the link when it is clicked while the app is dead, but nothing works. Basically this checks that the user is logged in (always is), then looks for a link.
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
    self.scene(scene, openURLContexts: connectionOptions.urlContexts)
    
    guard let _ = (scene as? UIWindowScene) else { return }

    if Auth.auth().currentUser != nil {
        
        if let userActivity = connectionOptions.userActivities.first {
          self.scene(scene, continue: userActivity)
        } else {
          self.scene(scene, openURLContexts: connectionOptions.urlContexts)
        }
        
        if let userActivity = connectionOptions.userActivities.first {
            if let incomingURL = userActivity.webpageURL {
                _ = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamicLink, error) in
                    guard error == nil else { return }
                    if let dynamicLink = dynamicLink {
                        self.handleIncomingDynamicLink(dynamicLink)
                    }  else {
                       print("Something went wrong")
                       let storyboard = UIStoryboard(name: "Main", bundle: nil)
                       let initialViewController = storyboard.instantiateViewController(withIdentifier: "RootVC")
                       self.window?.rootViewController = initialViewController
                   }
                }
            }
        } else {

            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let initialViewController = storyboard.instantiateViewController(withIdentifier: "RootVC")
            self.window?.rootViewController = initialViewController
        }
    }
    
}

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        
    if let url = URLContexts.first?.url {
        _ = DynamicLinks.dynamicLinks().handleUniversalLink(url) { (dynamicLink, error) in
        guard error == nil else { return }
        if let dynamicLink = dynamicLink {
        //your code for handling the dynamic link goes here
            self.handleIncomingDynamicLink(dynamicLink)
            }
        }
    }
}

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    if let incomingURL = userActivity.webpageURL {
        
        _ = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamicLink, error) in
            guard error == nil else {
                print(error!.localizedDescription)
                return
            }
            if let dynamicLink = dynamicLink {
                self.handleIncomingDynamicLink(dynamicLink)
            } else {
                print("Soemthing went wrong")
            }
        }
    }
}
4

2 回答 2

2

我发现出了什么问题。该问题与 SceneDelegate 无关 - 我处理链接的方式被另一个网络调用覆盖。

我如何解决这个问题,因为从冷启动打开应用程序时看不到打印语句,方法是将消息保存到 UserDefaults,然后创建弹出警报以在应用程序加载后读取保存的消息。

最后,我确实需要通过 AppDelegate 转移到 SceneDelegate。

于 2020-07-09T22:26:48.760 回答
-1

我无法对此进行测试,但它可以帮助在应用程序关闭时处理动态链接。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    if let userActivityDict = launchOptions?[.userActivityDictionary] as? [UIApplication.LaunchOptionsKey : Any],
        let userActivity = userActivityDict[.userActivityType] as? NSUserActivity {

        // execute the dynamic url code here using the userActivity
    }

    return true
}
于 2020-07-06T14:32:24.780 回答