Application crashing in iOS 12.0

1.1k views Asked by At

Application crashing in iOS 12.0 before iOS 12.0 it was working properly, I have googled it but did not get any solution for below crash log.

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key shouldAlwaysAlertWhileAppIsForeground.'

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

Did anyone fix such kind of issues?

1

There are 1 answers

1
arturdev On

In iOS12 the shouldAlwaysAlertWhileAppIsForeground keyPath is removed and no longer supported.


To achieve that on 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
    }
}