[英]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:这是预期的工作流程:
Links
on widget用户点击小部件上的Links
ViewController
, where ViewController
is the entry VC when users open the app)打开应用程序(显示ViewController
,其中ViewController
是用户打开应用程序时的入口 VC)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.