【问题标题】:SwiftUI WidgetKit: How to open specific viewController when app is terminatedSwiftUI WidgetKit:如何在应用程序终止时打开特定的 viewController
【发布时间】:2020-10-08 02:42:39
【问题描述】:

我正在使用 WidgetKit 为 iOS 14 构建小部件,并希望在用户点击小部件上的不同 Links 时打开我的应用程序的特定 viewControllers。仅当我的应用程序在后台时,应用程序的打开才有效。当应用程序终止时,它只会打开应用程序。

这是预期的工作流程:

  1. 用户在小部件上点击Links
  2. 打开应用(显示ViewController,其中ViewController是用户打开应用时的入口VC)
  3. 现在CameraController

我当前的实现仅在应用处于后台时才实现第 3 步。应用程序终止时如何获得相同的结果。

代码:

//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)
        }            
    }
}

【问题讨论】:

    标签: ios swift swiftui widgetkit


    【解决方案1】:

    在你的 appDelegate 中实现

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

    在那里处理你的options.urlContexts

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-01
      • 2019-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-09
      • 1970-01-01
      相关资源
      最近更新 更多