【问题标题】:Opening content from URL Scheme when App is closed应用关闭时从 URL Scheme 打开内容
【发布时间】:2020-08-14 02:02:29
【问题描述】:

我的问题

我正在我的应用程序中实现 URL 方案,当应用程序位于前台或后台时,它们总体上工作正常。但是,我注意到,当它完全关闭并且另一个应用程序尝试使用我的 URL(例如 app:page?image=1 )访问内容时,它通常会正常工作,它只是打开应用程序,但内容永远不会被捕获。

我的方法

我在 AppDelegate 和 SceneDelegate 方法中都设置了代码

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:])

还有

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {

期望的行为

当应用程序在后台、前台或关闭时打开

实际行为

它只在前台或后台时打开

【问题讨论】:

    标签: ios swift appdelegate url-scheme uiscenedelegate


    【解决方案1】:

    要处理传入的 URL,我们只需在 scene(_:willConnectTo:options:)scene(_:openURLContexts:) 委托方法中调用此函数:

    如果应用程序关闭:

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let _ = (scene as? UIWindowScene) else { return }
        
        
        // Since this function isn't exclusively called to handle URLs we're not going to prematurely return if no URL is present.
        if let url = connectionOptions.urlContexts.first?.url {
            handleURL(url: url)
        }
    }
    

    应用是在后台还是前台

    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        // Get the first URL out of the URLContexts set. If it does not exist, abort handling the passed URLs and exit this method.
        guard let url = URLContexts.first?.url else {
            return NSLog("No URL passed to open the app")
        }
    
    
        
        handleURL(url: url)
    }
    

    您可以返回以下文章了解有关场景委托和 URL 方案的更多信息:Custom URL Schemes in iOS

    【讨论】:

      【解决方案2】:

      由于您的应用当前是 not running,因此将使用这些启动选项启动。即这些选项将被传递给willFinishLaunchingWithOptions: / didFinishLaunchingWithOptions:。将您的代码添加到这些方法之一。

      有关更多信息,请阅读有关如何Respond to the Launch of Your App,或者更具体地说是Determine Why Your App Was Launched 的文档。

      编辑:

      正如下面@paulw11 所评论的,场景委托的工作方式不同,必须单独处理。

      但是,在Respond to Scene-Based Life-Cycle Events 部分,最后一点是:

      除了与场景相关的事件外,您还必须响应 使用您的 UIApplicationDelegate 对象启动您的应用程序。为了 有关在应用程序启动时做什么的信息,请参阅 Responding to the Launch of Your App

      所以我假设,我们仍然需要处理 willdidFinishLaunchingWithOptions / didFinishLaunchingWithOptions 中的启动。

      【讨论】:

      • 另外,请注意,如果您采用了较新的 SceneDelegate 方法,则选项将传递给您的场景委托,而不是您的应用委托
      • @Paulw11 感谢您的意见。我还没有使用场景。但是,Respond to Scene-Based Life-Cycle Events 最后一点是:除了与场景相关的事件之外,您还必须使用您的 UIApplicationDelegate 对象来响应您的应用程序的启动。有关在应用启动时要做什么的信息,请参阅响应应用的启动。。所以我假设,我们仍然需要在will/did didFinishLaunchingWithOptions 中处理 launch,这是 OP 问题中缺少的部分。
      • 是的,你仍然需要一个应用代理,但如果你已经实现了场景代理,那么 NSUserActivity 不会在 iOS13 上传递给你的应用代理。如果您支持早期版本的 iOS 并且有场景委托,那么您需要在两个地方处理活动
      • 绝妙的答案!这个逻辑也适用于通用链接:scene(_:continue:) 在冷应用启动时不会被调用,相关的UserActivity 被传递给scene(_:willConnectTo:options:),在options.userActivities
      猜你喜欢
      • 2012-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2015-11-03
      相关资源
      最近更新 更多