[英]iOS push notification click causes app to crash
我的推送通知点击有问题。 每次用户单击通知时,应用程序将崩溃,而不是将用户重定向到指定页面。
这部分代码导致错误“无法将类型'appname.LaunchScreenController'的值强制转换为'UINavigationController'” :
let rootViewController = self.window!.rootViewController as! UINavigationController
并且此代码将导致致命错误:解开Optional值时意外发现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)
}
提前致谢
RootViewController是UIViewController
子类,而不是UINavigationController
您必须处理null
值
改成
let rootViewController = self.window!.rootViewController
我将代码更改为此并且现在可以正常工作
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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.