应用程序崩溃在iOS 12.0中

Nil*_*esh 1 ios swift swift4.1 ios12

应用程序崩溃在iOS 12.0之前的iOS 12.0它运行正常,我用谷歌搜索它但没有得到任何解决方案下面的崩溃日志.

由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[setValue:forUndefinedKey:]:此类不是密钥值编码兼容的密钥shouldAlwaysAlertWhileAppIsForeground.

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

有人解决过这样的问题吗?

art*_*dev 6

在iOS12中,shouldAlwaysAlertWhileAppIsForegroundkeyPath将被删除,不再受支持.


要在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
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的回复,此问题可用的任何其他解决方案,因为我想在应用程序位于前台时显示通知. (3认同)