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. 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:
- User taps on
Links
on widget - Opens app (shows
ViewController
, whereViewController
is the entry VC when users open the app) - Present
CameraController
My current implementation achieved step 3 only when app is in the background. 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)
}
}
}