swiftuiviewcontrollernotificationsappdelegate

Open MapViewController and center on Lat/Long when user taps notification banner


So I'm having a tough time. I'm new to Swift but have been getting better. I have an app where users will receive a notification when something is placed on the map. What I'm trying to do is launch the MapViewController when the user taps on the notification, then center the map view on the Lat/Long passed in the notification.

The issue is, when the MapViewController loads, all of it's IBOutlet references from the storyboard are nil so the app crashes. How do I pull this off? (even chatGPT is hitting a brick wall :-)

I'm currently calling the MapViewController from the AppDelegate.swift file in the the 'didRecieve' method of the 'userNotificationCenter' function. Here's what that looks like:

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse) async {

    guard let window = UIApplication.shared.windows.first else {return}
            
    if let rootViewController = window.rootViewController as? UITabBarController {
        // Set the desired tab index to switch to
        let desiredTabIndex = 1
        
        // is tab index in range
        guard desiredTabIndex < rootViewController.viewControllers?.count ?? 0 else {return}
        
        // assign the desired VC
        let selectedViewController = rootViewController.viewControllers?[desiredTabIndex]
        
        // Check if the selected VC is the MapViewController
        if let notificationViewController = selectedViewController as? MapViewController {
            // Create an instance of the map VC
            let notificationViewController = MapViewController()
            
            // pass to the destination view controller
            notificationViewController.notificationData = notificationData
            
            // Present the destination view controller
            rootViewController.present(notificationViewController, animated: true, completion: nil)
        }
    }

}

Solution

  • For the first part (launching map viewcontroller), I realized all of my VC's were controlled by a tabbarcontroller. So all I had to do was change the tab:

    guard let window = UIApplication.shared.windows.first else {return}
        
    if let tabController = window.rootViewController as? UITabBarController{
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        if let myViewController = storyboard.instantiateViewController(withIdentifier: "MapViewController") as? MapViewController {
            tabController.selectedIndex = 1
        }
    }
    

    For the second part, I used the following:

    let center = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    let mRegion = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: locSpan, longitudeDelta: locSpan)
    self.mapView.setRegion(mRegion, animated: true)