iOS13:如果应用程序未运行,则通用链接不会打开

问题描述

希望得到任何帮助。我们已经在我们的应用中实现了通用链接的处理,但我正在努力解决以下问题:

  • 通用链接在应用在后台运行时打开(工作正常)

  • 在安装了 iOS13 的设备上运行时,打开通用链接只有在应用程序在后台运行时才能正常工作。如果已终止,则在点击 链接应用程序正在启动但未调用方法

    application(continue userActivity:..,restoreHandler:..)

    有什么想法吗?欣赏!

在此处输入代码

var window: UIWindow?   
var tabBarController1: UITabBarController?

func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool
{
    presentAppLaunchVC()
    return true
}

func presentVC(navController : UINavigationController)
{
    if var topController = UIApplication.shared.keyWindow?.rootViewController {
        while let presentedViewController = topController.presentedViewController {
            topController = presentedViewController
        }
        topController.present(navController,animated: false,completion: nil)
    }
}

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

    if userActivity.activityType == NSUserActivityTypebrowsingWeb
    {
        guard let url = userActivity.webpageURL else {
            return false
        }
        if !isValidDeepLink(web_url: url)
        {
            UIApplication.shared.open(url,options: [:],completionHandler: nil)
        }
        else
        {
            scrapDeepLinkingUrl(url : url)
        }
    }
    return true
}
func isValidDeepLink(web_url :URL) -> Bool
{
    
    guard let components = URLComponents(url : web_url,resolvingAgainstBaseURL : true) else {
        return  false
    }
    guard let host = components.host else {
        return false
    }
    switch host {
    case "www.domain.com":
        return true
    default:
        return false
    }
}
func scrapDeepLinkingUrl(url : URL)
{
    }
    else
    {
        presentAppLaunchVC()
    }
}
func presentAppLaunchVC()
{
    let storyBoard = UIStoryboard(name: storyboard_name,bundle: nil)
    let screen = storyBoard.instantiateViewController(withIdentifier: identifier)
    if identifier == "dashboardVC" {
        tabBarController1 = screen as? UITabBarController
    }
    self.window?.rootViewController = screen
}
        

解决方法

您还需要检查 didFinishLaunchingWithOptions 方法中的 URL。

它可以是一个 URL: launchOptions[UIApplicationLaunchOptionsURLKey]

或者它可以是一个通用链接: launchOptions[UIApplicationLaunchOptionsUserActivityDictionaryKey]

,

我要做的是添加 conditional scene delegate 支持。这样,您将在 scene(_:willConnectTo:) 中收到消息。好的,这需要更多的工作,但您需要与 iOS 13 及更高版本中的原生场景支持保持同步,现在似乎是时候这样做了。