核心数据和Userdefaults不存储数据

时间:2020-01-27 12:34:57

标签: ios swift core-data default

关闭我的应用程序时,核心数据和UserDefaults不存储数据。

我的应用程序关闭时会收到通知,因此我将其存储在核心数据中,并且默认情况下会做一些事情。

打开应用程序时,它会存储所有信息,但是只有关闭了应用程序时,它才无法工作,任何想法都可以这样做。

我将数据存储在以下函数中。

  func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            let response = notification.request.content.userInfo["aps"] as! NSDictionary
             //here i will send response will store to core date
            self.defaults.set("1", forKey: "notifyid")
            completionHandler([.alert, .badge, .sound])
        }
    } 

1 个答案:

答案 0 :(得分:0)

您将其存储在错误的功能中。仅当应用程序位于前景中时才会调用Will Present。因此,当应用程序在后台运行时,它将无法正常工作,而是使用了Will Present功能而不是接收了远程功能:

func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {
    let response = notification.request.content.userInfo["aps"] as! NSDictionary
    self.defaults.set("1", forKey: "notifyid")    
}

注意:请确保在后台模式下的应用程序功能中,您的远程通知和后台获取。否则它将无法正常工作。

相关问题