【问题标题】:Application crashing in iOS 12.0iOS 12.0 中的应用程序崩溃
【发布时间】:2018-09-27 06:43:14
【问题描述】:

应用程序在 iOS 12.0 之前的 iOS 12.0 中崩溃,它工作正常,我用谷歌搜索了它,但没有得到以下崩溃日志的任何解决方案。

由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类与键 shouldAlwaysAlertWhileAppIsForeground 的键值编码不兼容。”

let content = UNMutableNotificationContent()
//App crash on below line
content.setValue(true, forKeyPath: "shouldAlwaysAlertWhileAppIsForeground")

有人解决过此类问题吗?

【问题讨论】:

  • UNMutableNotificationContent 似乎没有正式的shouldAlwaysAlertWhileAppIsForeground 属性,这可能是您的应用之前意外运行。
  • 嘿@Nilesh!您是否找到任何解决此问题的方法?
  • 我只是在那里添加条件 if #available(iOS 12.0, *) { }else {content.setValue(true, forKeyPath: "shouldAlwaysAlertWhileAppIsForeground") }

标签: ios swift ios12 swift4.1


【解决方案1】:

在 iOS12 中,shouldAlwaysAlertWhileAppIsForeground keyPath 已被移除且不再受支持。


要在 iOS12 上实现:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    UNUserNotificationCenter.current().delegate = self
    ...
}
...
extension AppDelegate: UNUserNotificationCenterDelegate {
    // The method will be called on the delegate only if the application is in the foreground. 
    // If the method is not implemented or the handler is not called in a timely manner then the notification will not be presented. 
    // The application can choose to have the notification presented as a sound, badge, alert and/or in the notification list. 
    //This decision should be based on whether the information in the notification is otherwise visible to the user.
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.alert, .sound])// Will present an alert and will play a sound when a notification arrives
    }
}

【讨论】:

  • 感谢您的回复,任何其他可用于此问题的解决方案,因为我想在应用程序处于前台时显示通知。
猜你喜欢
  • 1970-01-01
  • 2017-01-17
  • 1970-01-01
  • 2012-03-27
  • 1970-01-01
  • 2013-07-07
  • 2016-09-22
  • 2016-02-25
  • 1970-01-01
相关资源
最近更新 更多