简体   繁体   English

SwiftUI WidgetKit:如何在应用程序终止时打开特定的 viewController

[英]SwiftUI WidgetKit: How to open specific viewController when app is terminated

I am building widgets for iOS 14 using WidgetKit and would like to open specific viewControllers of my app when users tap on different Links on the widget.我正在使用 WidgetKit 为 iOS 14 构建小部件,并希望在用户点击小部件上的不同Links时打开我的应用程序的特定viewControllers The opening of the app works only when my app is in the background.只有当我的应用程序在后台时,应用程序的打开才有效。 When the app is terminated, it simply opens the app only.当应用程序终止时,它只会打开应用程序。

Here's the expected workflow:这是预期的工作流程:

  1. User taps on Links on widget用户点击小部件上的Links
  2. Opens app (shows ViewController , where ViewController is the entry VC when users open the app)打开应用程序(显示ViewController ,其中ViewController是用户打开应用程序时的入口 VC)
  3. Present CameraController当前CameraController

My current implementation achieved step 3 only when app is in the background.我当前的实现仅在应用程序处于后台时才实现了第 3 步。 How do I achieve the same result when app is terminated.应用程序终止时如何获得相同的结果。

Code:代码:

//At Widget View
struct CameraWidgetView : View {
    var entry: CameraEntry
    
    var body: some View {
        VStack(alignment: .leading, spacing: 4) {
            
            Link(destination: URL(string: "scheme://camera")!, label: {
                Text("Camera")
            })
        }
        .widgetURL(URL(string: "scheme://camera"))
    }
}

//At my app's SceneDelegate.swift
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    guard let windowScene = (scene as? UIWindowScene) else { return }
    window = UIWindow(frame: UIScreen.main.bounds)
    
    if let item = URLContexts.first {
        let url = item.url

        if "\(url)" == "scheme://camera" {
            let vc = ViewController()
            let navController = UINavigationController(rootViewController: vc)
            
            window?.rootViewController = navController
            window?.makeKeyAndVisible()
            window?.windowScene = windowScene
            
            let cameraController = CameraController()
            let camNavController = UINavigationController(rootViewController: cameraController)
            navController.present(camNavController, animated: true, completion: nil)
        }            
    }
}

In your appDelegate, implement在您的 appDelegate 中,实现

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration

and there handle your options.urlContexts并处理您的options.urlContexts

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

相关问题 当应用终止或终止时,如何在点击推送通知后打开特定的控制器 - how to open a specific controller after tapping on push notification when the app is terminated or killed 从推送通知 IOS 打开应用程序时打开特定的 ViewController - Open specific ViewController when open app from push notification IOS 当主应用程序未打开时,Swift 如何从今天的扩展中打开特定的视图控制器 - Swift how to open specific viewcontroller from today extension when main app is not open 如何在 WidgetKit、SwiftUI 中更改午夜视图? - How to change view on midnight in WidgetKit, SwiftUI? SwiftUI WidgetKit:如何将视图锚定到顶部 - SwiftUI WidgetKit: How to anchor views to the top 如何使用 AppIntents 在 SwiftUI 应用程序中打开特定视图 - How to open specific View in SwiftUI app using AppIntents 如何从AppDelegate打开特定的ViewController? - How to open specific ViewController from AppDelegate? 如何从 SwiftUI 视图弹回特定的 ViewController(UIKit) - How Pop back to specific ViewController(UIKit) from SwiftUI view 如何从 TabBarController 打开特定的 ViewController - How to open specific ViewController from TabBarController 如何读取由 iOS WidgetKit 应用程序创建的文件? - How to read files created by the app by iOS WidgetKit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM