Сбой приложения в iOS 12.0

Сбой приложения в iOS 12.0 до iOS 12.0 он работал правильно, я погуглил, но не нашел решения для журнала сбоев ниже.

Завершение работы приложения из-за неперехваченного исключения «NSUnknownKeyException», причина: «[setValue: forUndefinedKey:]: этот класс не соответствует кодированию значения ключа для ключа shouldAlwaysAlertWhileAppIsForeground».

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

Кто-нибудь исправлял такие проблемы?


person Nilesh    schedule 27.09.2018    source источник
comment
UNMutableNotificationContent, похоже, официально не имеет свойства shouldAlwaysAlertWhileAppIsForeground, возможно, ваше приложение ранее работало случайно.   -  person Cristik    schedule 27.09.2018
comment
Привет, @Nilesh! Вы нашли способ обхода этой проблемы?   -  person manar    schedule 05.10.2018
comment
Я просто добавляю туда условие if #available (iOS 12.0, *) {} else {content.setValue (true, forKeyPath: shouldAlwaysAlertWhileAppIsForeground)}   -  person Nilesh    schedule 05.10.2018


Ответы (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
    }
}
person arturdev    schedule 27.09.2018
comment
Спасибо за ответ, любое другое решение, доступное для этой проблемы, потому что я хочу отображать уведомление, когда приложение находится на переднем плане. - person Nilesh; 27.09.2018