我的推送通知点击问题。每次用户点击通知时,应用程序都会崩溃,而不是将用户重定向到指定的页面。
这部分代码导致错误"无法转换类型' appname.LaunchScreenController'到' UINavigationController'" :
let rootViewController = self.window!.rootViewController as! UINavigationController
此代码会导致致命错误:在解包可选值时意外发现nil :
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
//receive the notifications
NotificationCenter.default.post(name: Notification.Name(rawValue: "MyNotificationType"), object: nil, userInfo: userInfo)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "NewsController") as! NewsViewController
let rootViewController = self.window!.rootViewController as! UINavigationController
rootViewController.pushViewController(vc, animated:true)
}
提前致谢
答案 0 :(得分:0)
RootViewController是UIViewController
的子类而非UINavigationController
您必须处理null
值
更改为
let rootViewController = self.window!.rootViewController
答案 1 :(得分:0)
我将代码更改为此,现在工作正常
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
NotificationCenter.default.post(name: Notification.Name(rawValue: "MyNotificationType"), object: nil, userInfo: userInfo)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "NewsController") as! NewsViewController
let nav = UINavigationController()
nav.pushViewController(vc, animated: true)
}