应用程序在iOS 12.0之前在iOS 12.0中崩溃,它工作正常,我已经用谷歌搜索了它,但没有为下面的崩溃日志找到任何解决方案。
由于未捕获的异常"NSUnknownKeyException"而终止应用,原因:"[ setValue:forUndefinedKey:]:此类不符合键值编码的键值编码,因为键应该始终警报而应用是前景。
let content = UNMutableNotificationContent()
//App crash on below line
content.setValue(true, forKeyPath: "shouldAlwaysAlertWhileAppIsForeground")
有人解决过此类问题吗?
在 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
}
}