0

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:

  1. User taps on Links on widget
  2. Opens app (shows ViewController, where ViewController is the entry VC when users open the app)
  3. 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)
        }            
    }
}
Koh
  • 2,687
  • 1
  • 22
  • 62

1 Answers1

0

In your appDelegate, implement

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

and there handle your options.urlContexts

Riskov
  • 844
  • 7
  • 12