iOS 12.0中的应用程序崩溃

时间:2018-09-27 06:43:15

标签: ios swift ios12 swift4.1

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

  

由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[setValue:forUndefinedKey:]:此类不符合密钥ShouldAlwaysAlertWhileAppIsForeground的键值编码要求。'

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

有人解决了这类问题吗?

1 个答案:

答案 0 :(得分:6)

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