从通知操作中呈现特定的视图控制器

Present a specific view controller from notification action

我正在处理本地通知,我正在尝试展示一个特定的 viewController 但我已经尝试了我在这个论坛中找到的内容,并且我在显示的视图中出现异常行为 this picture here: 这是 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
            })
        }
    }
}

代码有什么问题(尤其是redirectToVC()方法)? 任何帮助将不胜感激。

我刚刚找到了这个问题的答案。它基本上是从 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()
}

感谢

每当你必须在当前屏幕上显示一个特定的视图控制器时,无论你的视图控制器是通过导航推送还是可能被显示,在这两种情况下你都可以使用下面的代码来解决你的问题。

                    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)
                        }
                    }

您不能在导航控制器上显示 viewcontroller。您需要获得 topviewcontroller 才能展示。下面是执行此操作的代码:-

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

使用以下方法获取顶视图控制器:-

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
  }
}

而 launchViewController 函数是:-

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

在Swift 5

在 Appdelegate 中创建变量 Class:

    let window : UIWindow? = UIWindow()

并在需要重定向的地方使用以下函数:

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)
        }
    }
}