首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在主应用程序未打开时从今天的扩展打开特定的视图控制器

如何在主应用程序未打开时从今天的扩展打开特定的视图控制器
EN

Stack Overflow用户
提问于 2020-08-28 23:21:02
回答 1查看 617关注 0票数 2

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

我所做的:我已经设置了URL,并编写了OpenURL代码,如果按钮是按下TodayExtension视图控制器

代码语言:javascript
复制
    @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>) {中导航到特定视图控制器的代码。

代码语言:javascript
复制
    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!)
                }
            }
        }
    }

但就像我说过的,这只在主应用程序之前被打开时才起作用。如果主ap关闭了,我要怎么做才能做到这一点?

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

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

    }

但这不管用。

希望有人能帮我。

EN

回答 1

Stack Overflow用户

发布于 2020-10-23 11:48:44

最后我解决了我的问题。

我不得不在场景willConnectTo中加上一行..。职能:

代码语言:javascript
复制
    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)

        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63641970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档