techcoderx
techcoderx

Reputation: 602

Present a specific view controller from notification action

I'm working with local notifications and I'm trying to present a specific viewController but I have tried what I've found in this forum and I got an unusual behavior with the view shown in this picture here: And here's the source code of AppDelegate.swift:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    print("didReceive Method called")

    if response.actionIdentifier == "actionOne" {
        DispatchQueue.main.async(execute: {
            self.notificationAction1()
        })
    } else if response.actionIdentifier == "actionTwo" {
        DispatchQueue.main.async(execute: { 
            self.notificationAction2()
        })

    } else if response.actionIdentifier == "actionThree" {

    }
    completionHandler()
}

func notificationAction1() {
    redirectToVC()
}

func redirectToVC() {
    let toVC = VersesViewController()
    if self.window != nil && self.window?.rootViewController != nil {
        let rootVC = self.window?.rootViewController!
        if rootVC is UINavigationController {
            (rootVC as! UINavigationController).pushViewController(toVC, animated: true)
        } else {
            rootVC?.present(toVC, animated: true, completion: { 
                //Do something
            })
        }
    }
}

What is wrong with the code (especially the redirectToVC() method)? Any help will be appreciated.

Upvotes: 2

Views: 3044

Answers (4)

Harish Rathuri
Harish Rathuri

Reputation: 72

Whenever you have to present a specific view controller on current screen, it's not matter if you view-controller pushed through navigation or might be presented, in both case you can use below code to solve you problem.

var appdelgateObj = UIApplication.shared.delegate as! AppDelegate
if let destinationVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: “ToPresentVC”) as? ToPresentVC {
    if let window = appdelgateObj.window , let rootViewController = window.rootViewController {
        var currentController = rootViewController
        while let presentedController = currentController.presentedViewController {
            currentController = presentedController
        }
        currentController.present(destinationVC, animated: true, completion: nil)
    }
}

Upvotes: 1

Anil Kumar
Anil Kumar

Reputation: 1984

In Swift 5

Create variable in Appdelegate Class:

    let window : UIWindow? = UIWindow()

and use below function from where you need to redirect:

func navigateToVCOne() {
    if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "pbVC") as? ProblemViewController {
        if let window = self.window, let rootViewController = window.rootViewController {
            var currentController = rootViewController
            while let presentedController = currentController.presentedViewController {
                currentController = presentedController
            }
            currentController.present(controller, animated: true, completion: nil)
        }
    }
}

Upvotes: 0

iOS Developer
iOS Developer

Reputation: 472

You can not present a viewcontroller on navigation controller. You need to get topviewcontroller to present. Below is the code to do that:-

    guard let topVC = UIApplication.getTopViewController() else {
        return
    }
    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
        self.launchViewController(topVC)
    }

Get Top View controller using:-

extension UIApplication {

class func getTopViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {

    if let nav = base as? UINavigationController {
        return getTopViewController(base: nav.visibleViewController)

    } else if let tab = base as? UITabBarController, let selected = tab.selectedViewController {
        return getTopViewController(base: selected)

    } else if let presented = base?.presentedViewController {
        return getTopViewController(base: presented)
    }
    return base
  }
}

And launchViewController func is:-

class func launchViewController(_ sender: UIViewController) {
        let vc: Viewcontroller = storyboard.instantiateViewController(withIdentifier: "Viewcontroller") as! Viewcontroller
        sender.navigationController?.present(vc, animated: true, completion: nil)
    }

Upvotes: 0

techcoderx
techcoderx

Reputation: 602

I just found an answer to this. It's basically presenting a view controller from AppDelegate.

func redirectToVC() {
    let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let initialViewController: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "versesVC") as UIViewController
    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()
}

Thanks to Opening view controller from app delegate using swift

Upvotes: 3

Related Questions