简体   繁体   English

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

[英]Swift how to open specific viewcontroller from today extension when main app is not open

I have created a today extension with some UIButtons and I want to open a specific view controller when the user taps a button.我用一些 UIButtons 创建了一个今天的扩展,我想在用户点击一个按钮时打开一个特定的视图控制器。 It works right now when the main app is open, but when it is closed then it will only open the main app and dont navigate to my specific view controller.当主应用程序打开时它现在可以工作,但是当它关​​闭时它只会打开主应用程序并且不会导航到我的特定视图控制器。

What I have done: I have set up the URL Sheme and wrote the OpenURL Code if button is pressed in TodayExtension View Controller .我所做的:如果在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)
            }
        }

In SceneDelegate I wrote the code to navigate to specific view controller in func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {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!)
                }
            }
        }
    }

But like i said this only works when the main app is opened before.但就像我说的那样,这只适用于之前打开主应用程序的情况。 What do I have to do that this works even if the main ap is closed?即使主 ap 关闭,我该怎么做才能正常工作?

With the help from Google I figured out that I have to set up the func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { function but I dont know how.在谷歌的帮助下,我发现我必须设置func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {功能,但我不知道如何。 What I have wrote is我写的是

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

    }

but this dont work.但这不起作用。

Hope someone can help me.希望可以有人帮帮我。

Finally I solved my problem.最后我解决了我的问题。

I had to add a line in scene willConnectTo.. function:我不得不在场景 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)

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 点击扩展程序(今天的扩展程序)后是否可以打开特定的viewcontroller(包含应用程序)? - is it possible to open specific viewcontroller(containing app) after tap on extension(today extension)? Swift中主要应用的iOS 10 Open Today Widget - iOS 10 Open Today Widget from main App in Swift 从推送通知 IOS 打开应用程序时打开特定的 ViewController - Open specific ViewController when open app from push notification IOS 如何使用Swift在我的应用程序的“今日扩展”中的Safari中打开url - How to open a url in Safari from my Today Extension within my app using Swift 打开包含今日扩展的应用程序时崩溃 - Crash when open containing app from today extension SwiftUI WidgetKit:如何在应用程序终止时打开特定的 viewController - SwiftUI WidgetKit: How to open specific viewController when app is terminated 来自Today Extension的打开网址 - Open url from Today Extension 从Today扩展中打开特定的ViewController - Opening specific ViewController from Today Extension 如何从AppDelegate打开特定的ViewController? - How to open specific ViewController from AppDelegate? 如何从 TabBarController 打开特定的 ViewController - How to open specific ViewController from TabBarController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM