swiftuiviewcontrolleruitabbarcontrollertoday-extensionuiscenedelegate

Open specific UITabBarController / ViewController from SceneDelegate.swift


I have a Today Extension which has a UITableView.

When the user taps one of the items in the UITableView, it opens the containing app with a URL Scheme and passes some data so that the containing app can open a specific view for the item the user tapped.

The problem is, from SceneDelegate.swift I can handle the URL Scheme but it seems I can't make a specific UITabBarController show and open the detail view for the item tapped by the user.

Please note that I'm not trying to create them; they're already created from the storyboard and working. I just want it to 'Navigate' automatically as if the user had tapped the tableview item.

The below code is what I've tried:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    // This part, I'm handling URL Scheme which is working fine
    let url = URLContexts.first!.url
    let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false)
    let items = urlComponents?.queryItems
    var deepLinkArray: [URLQueryItem] = []
    deepLinkArray = items!


    // From here I will use datas I got from URL
    if let statusValue = deepLinkArray[0].value, let indexPathSection = deepLinkArray[1].value, let indexPathRow = deepLinkArray[2].value {
        todoStatusValueURL = Int(statusValue)!
        indexPathSectionURL = Int(indexPathSection)!
        indexPathRowURL = Int(indexPathRow)!



        // Here I want to show one of UITabBarController
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let snoozedVC = storyboard.instantiateViewController(withIdentifier: "SnoozedViewController") as! SnoozedViewController
        snoozedVC.self.tabBarController?.selectedIndex = todoStatusValueURL! // And this is not showing what I want


        // Additionally I want to show ItemDetailViewController as if user tapped the item in the tableview, and below code is also not working
        let detailVC = storyboard.instantiateViewController(withIdentifier: "DetailVC") as! DetailVC
        snoozedVC.navigationController?.pushViewController(detailVC, animated: true)

}

Solution

  • First check if snoozedVC and TabBarController for that specific snoozedVC exist.. it will give you idea whats wrong with your code ... then move forward according to its result to check which part of your code is not working

    if let snoozedVC = storyboard.instantiateViewController(withIdentifier: "SnoozedViewController") as? SnoozedViewController {
        if let tabBarController = snoozedVC.tabBarController {
    
                tabBarController.selectedIndex = todoStatusValueURL!
    
                }  else {
                     print("tab controller not found")
                 }
               } else {
                     print("SnoozedViewController not found")
                 }
              }