【问题标题】:Swift how to open specific viewcontroller from today extension when main app is not open当主应用程序未打开时,Swift 如何从今天的扩展中打开特定的视图控制器
【发布时间】:2020-12-17 20:54:15
【问题描述】:

我已经用一些 UIButtons 创建了一个 Today 扩展,我想在用户点击一个按钮时打开一个特定的视图控制器。当主应用程序打开时它现在可以工作,但是当它关​​闭时它只会打开主应用程序并且不会导航到我的特定视图控制器。

我做了什么: 如果在 TodayExtension View Controller 中按下按钮,我已经设置了 URL Sheme 并编写了 OpenURL 代码。

    @objc func buttonClick(sender: UIButton) {
        let tag = sender.tag
        if let url = URL(string: "OpenURL://\(tag)")
            {
                self.extensionContext?.open(url, completionHandler: nil)
            }
        }

SceneDelegate中,我编写了导航到func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {中的特定视图控制器的代码

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

    if let url = URLContexts.first?.url {

        if url.scheme == "OpenURL" {
            
        guard let rootViewController = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window?.rootViewController else {
                return
        }

        let storyboard = UIStoryboard(name: "Groups", bundle: nil)

        if  let rootVC = storyboard.instantiateViewController(withIdentifier: "Create") as? CreateSingleGroupViewController,
            let tabBarController = rootViewController as? UITabBarController,
            let navController = tabBarController.selectedViewController as? UINavigationController {
                    navController.pushViewController(rootVC, animated: true)
            
            rootVC.buttonTag = Int(url.host!)
                }
            }
        }
    }

但就像我说的那样,这仅在之前打开主应用程序时才有效。 即使主应用程序关闭,我该怎么做才能正常工作?

在 Google 的帮助下,我发现我必须设置 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { 函数,但我不知道如何设置。我写的是

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let _ = (scene as? UIWindowScene) else { return }
        self.scene(scene, openURLContexts: connectionOptions.urlContexts)

    }

但这不起作用。

希望有人可以帮助我。

【问题讨论】:

    标签: ios swift today-extension uiscenedelegate


    【解决方案1】:

    终于解决了我的问题。

    我不得不在场景中添加一行 willConnectTo.. 函数:

        func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
            guard let _ = (scene as? UIWindowScene) else { return }
            
            if let url = connectionOptions.urlContexts.first?.url {
                UIApplication.shared.open(url)
                self.scene(scene, openURLContexts: connectionOptions.urlContexts)
    
            }
        }
    

    【讨论】:

    • 嗨@Mina!很好的问题和回复,请您在解决方案中添加hole sn-p代码!
    • 嗨@wazowski!对不起,我已经发布了我现在使用的所有代码。我从问题中添加到代码中的唯一内容是if let url = connectionOptions.urlContexts.first?.url { UIApplication.shared.open(url) 行,您可以在我的回答中看到。希望这对您有所帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 2015-08-16
    相关资源
    最近更新 更多