簡體   English   中英

iOS 13 > 當應用程序被殺死且不在后台時,通用應用程序鏈接不起作用

[英]iOS 13 > Universal App Links does not work when app is killed and not in background

知道為什么從通過Apple-App-Site-Association定義的 iOS 13 應用程序鏈接(通用鏈接)開始停止工作嗎?

我在 ApplicationDelegate 和 SceneDelegate 中有 2 個實現。 現在僅適用於 SceneDelegate 中的實現,並且僅當應用程序處於后台時,如果我殺死應用程序,則不會調用方法 continueUserActivity 。 我添加了觸覺反饋來跟蹤這個方法調用,但它永遠不會在 ActivityDelegate 或 SceneDelegate 中被調用。

// MARK: - Universal Links support
extension SceneDelegate {

    func scene(_ scene: UIScene, willContinueUserActivityWithType userActivityType: String) {
        print("[Scene] Will continue user activity: ", userActivityType)
        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(.success)

    }

    func scene(_ scene: UIScene, didFailToContinueUserActivityWithType userActivityType: String, error: Error) {
        print("[Scene] Did fail to continue user activity: ", userActivityType)
    }

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

        print("[Scene] Application continue user activity...")

        if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
            if let url = userActivity.webpageURL {
                if !present(url: url) { UIApplication.shared.open(url, options: [:]) }
            }
        }
    }

和申請委托案例

// MARK: - Universal Links support
extension AppDelegate {

    func application(_ application: UIApplication, willContinueUserActivityWithType userActivityType: String) -> Bool {
        print("[App] Will continue user activity: ", userActivityType)
        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(.warning)
        return true
    }

    func application(_ application: UIApplication, didFailToContinueUserActivityWithType userActivityType: String, error: Error) {
        print("[App] Did fail to continue user activity: ", userActivityType)
    }

    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {

        print("[App] Application continue user activity...")

        if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
            if let url = userActivity.webpageURL {
                if !present(url: url) { UIApplication.shared.open(url, options: [:]) }
            }
        }
        return true
    }

正在打開應用程序,但未調用方法,我無法導航到應用程序內的相應屏幕。

好的,我發現你必須做這樣的事情

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
   
    if let userActivity = connectionOptions.userActivities.first {
      self.scene(scene, continue: userActivity)
    }
  }

對我來說,唯一的解決方案是刪除 SceneDelegate 並將代碼放入 AppDelegate 中,只要應用程序被殺死或在后台,它就可以工作。 如果您不使用 SwiftUI,則可以刪除 SceneDelegate

對於沒有 App 或 SceneDelegate 的 SwiftUI 2.0 用戶。 我的例子是關於 Firebase 通用鏈接,它應該在打開后做一些事情。 有一個非常好的方法叫做onOpenURL ,您可以使用它。

 var body: some Scene {
    
    WindowGroup {
        ContentView(shouldPresentThankYouView: $shouldPresentThankYouView).onOpenURL { url in
            
            _ = DynamicLinks.dynamicLinks().handleUniversalLink(url) { (dynamicLink, error) in
                guard error == nil else{
                    print("Found an error! \(error!.localizedDescription)")
                    return
                }
                if let dynamicLink = dynamicLink {
                    shouldPresentThankYouView = true
                    self.handleIncomingDynamicLink(dynamicLink)
                }
            }
        }
      } 
     }

如果用戶安裝了該應用程序,一旦他單擊該應用程序,就會出現感謝視圖。 希望它可以幫助您更具體地使用 Firebase 鏈接來處理通用鏈接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM