[英]How to open Viewcontroller from Appdelegate?
I use firebase to send a message to ios device, I debug, I have received data payload in Appdelegate in func 我使用firebase向ios设备发送消息,我进行调试,我在func的Appdelegate中收到了数据有效负载
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
I want to do how to open different view controllers based on that data, which means that when I click on the message, I will go to the corresponding view controllers. 我想根据该数据打开不同的视图控制器,这意味着当我单击消息时,将转到相应的视图控制器。 I used the code below in Appdelegate but failed
我在Appdelegate中使用了以下代码,但失败了
let sb = UIStoryboard(name: "Main", bundle: nil)
let otherVC = sb.instantiateViewController(withIdentifier: "UserNotLoginViewController") as! UserNotLoginViewController
self.window?.rootViewController = otherVC;
Declare this function in your appDelegate and then use it to change rootViewController. 在您的appDelegate中声明此函数,然后使用它更改rootViewController。
public func makeRootVC(vcName : String) {
let vc = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: vcName)
let navigation = UINavigationController(rootViewController: vc)
navigation.navigationBar.isHidden = true
self.window?.rootViewController = navigation
}
usage: 用法:
self.makeRootVC("YourViewControllerStoryboardID")
when you recive notification in didReceiveRemoteNotification delegate then call the function to pushview to nextviewcontroller. 当您在didReceiveRemoteNotification委托中接收通知时,然后将函数推入pushview到nextviewcontroller。
func application(_ application: UIApplication, didReceiveRemoteNotification
data: [AnyHashable : Any]) {
let state: UIApplicationState = UIApplication.shared.applicationState
if state == .background {
// background
pushToPage(data: data)
}
}
func pushToPage(data:[AnyHashable : Any]){
if let appDelegate = UIApplication.shared.delegate as? AppDelegate,
let window = appDelegate.window {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController =
storyBoard.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController
window.rootViewController = nextViewController
}
}
Here it is 这里是
func handlePushNotification(userInfo: [String: Any]) {
guard let notificationType = userInfo["nt"] as? String else {
return
}
if notificationType.toInt() == 1 {
self.navigateToViewController1()
} else if notificationType.toInt() == 2 {
self.navigateToViewController2()
}
}
And for navigation, you can use this below function 对于导航,您可以使用以下功能
fileprivate func navigateToViewController1() {
if let rootViewController = self.window?.rootViewController as? UINavigationController {
if let _ = rootViewController.topViewController as? VC1 {
let vc = AppStoryboard.Main.viewController(viewControllerClass: VC3.self)
rootViewController.pushViewController(vc, animated: true)
}
}
}
fileprivate func navigateToViewController2() {
if let rootViewController = self.window?.rootViewController as? UINavigationController {
if let homeVC = rootViewController.topViewController as? VC2 {
}
}
}
Still, you face any issue so please let me know. 不过,您仍然遇到任何问题,请让我知道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.