【问题标题】:iOS push notification click causes app to crashiOS推送通知点击导致应用程序崩溃
【发布时间】:2018-02-16 00:22:54
【问题描述】:

我的推送通知点击有问题。每次用户点击通知时,应用程序都会崩溃,而不是将用户重定向到指定页面。

这部分代码导致错误“无法将'appname.LaunchScreenController'类型的值转换为'UINavigationController'”

let rootViewController = self.window!.rootViewController as! UINavigationController

这段代码会导致fatal error: unexpectedly found nil while unwrapping an Optional value

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

提前致谢

【问题讨论】:

    标签: ios xcode swift3 onesignal


    【解决方案1】:

    RootViewController 是 UIViewController 的子类而不是 UINavigationController 您必须处理您的 null

    改成

    let rootViewController = self.window!.rootViewController
    

    【讨论】:

    • 我注意到了,但我想访问作为 UINavigationController 成员的“pushViewController”函数。因此,我使用 UINavigationController。
    • 您的应用程序是否嵌入了 UINavigationController?
    • 很抱歉,我真的很陌生。嵌入是什么意思?我已经用错误日志编辑了我的问题。
    • 表示基于导航的应用程序或基于标签的应用程序,您关注的是哪一个,您在哪里设置rootviewcontroller?
    【解决方案2】:

    我把代码改成这个,现在可以正常工作了

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2018-12-08
      • 2021-12-31
      • 2020-11-25
      相关资源
      最近更新 更多