0
A
回答
1
我正在寻找如何几天的做到这一点,才能够找到谁存储UILocalNotificaations的人NSUserDefaults。将这些保存在NSUserDefaults对我来说似乎是错误的,因为它应该用于小旗子。我刚才终于想出了如何在CoreData中存储通知。这是使用的Xcode 7.3.1和2.2雨燕
首先你需要在你的CoreDataModel 创建一个新的实体,然后单属性添加到它。属性应该是二进制数据我将我的表/实体命名为“ManagedFiredNotifications”,我的属性为“notification”。它应该看起来像这样:
上面的问题链接的图像。
接下来,需要添加扩展到UILocalNotification它应该是这样的:
extension UILocalNotification {
func save() -> Bool {
let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
let firedNotificationEntity = NSEntityDescription.insertNewObjectForEntityForName("ManagedFiredNotifications", inManagedObjectContext: appDelegate!.managedObjectContext)
guard appDelegate != nil else {
return false
}
let data = NSKeyedArchiver.archivedDataWithRootObject(self)
firedNotificationEntity.setValue(data, forKey: "notification")
do {
try appDelegate!.managedObjectContext.save()
return true
} catch {
return false
}
}
}
现在保存的通知,所有你需要做的就是调用
UILocalNotification.save()
在您想要保存的通知。我的通知被命名为“通知”,所以我会叫notification.save()
检索所需这样
func getLocalFiredNotifications() -> [UILocalNotification]? {
let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)!.managedObjectContext
let firedNotificationFetchRequest = NSFetchRequest(entityName: "ManagedFiredNotifications")
firedNotificationFetchRequest.includesPendingChanges = false
do {
let fetchedFiredNotifications = try managedObjectContext.executeFetchRequest(firedNotificationFetchRequest)
guard fetchedFiredNotifications.count > 0 else {
return nil
}
var firedNotificationsToReturn = [UILocalNotification]()
for managedFiredNotification in fetchedFiredNotifications {
let notificationData = managedFiredNotification.valueForKey("notification") as! NSData
let notificationToAdd = NSKeyedUnarchiver.unarchiveObjectWithData(notificationData) as! UILocalNotification
firedNotificationsToReturn.append(notificationToAdd)
}
return firedNotificationsToReturn
} catch {
return nil
}
}
注意它返回UILocalNotifications阵列的方法的通知。
当检索这些,如果你打算删除其中的一些,然后储存该列表时,会再次让他们这样的工作,你应该将其删除:
func loadFiredNotifications() {
let notifications = StudyHelper().getLocalFiredNotifications()
if notifications != nil {
firedNotifications = notifications!
} else {
// throw an error or log it
}
classThatRemoveMethodIsIn().removeFiredLocalNotifications()
}
我希望这可以帮助别人谁了我试图实现这个的同样的问题。
相关问题
- 1. 如何在CoreData中保存NSValue的NSMutableArray?
- 2. 如何在CoreData中保存位置
- 3. 如何在CoreData中保存CoreData的内容6
- 4. CoreData - 关于何时保存
- 5. 将NSManagedObjectID保存在CoreData中
- 6. 将NSDictionary保存在coreData中
- 7. 在CoreData中保存对象
- 8. CoreData保存错误
- 9. CoreData永久保存?
- 10. 永久保存CoreData
- 11. 保存NSDate到CoreData
- 12. 如何将NSAttributedString保存到CoreData
- 13. 如何处理CoreData保存错误?
- 14. 如何将UIStepper保存到CoreData(swift 3)
- 15. 如何将数据保存到coreData
- 16. 如何用UIPickerView保存CoreData,然后用UITableView加载CoreData?
- 17. 在UITableViewController中保存CoreData,但UISlider数据仅在原型单元格中 - 我如何将它保存到CoreData中
- 18. 在XMPPFrameWork中将XMPPMessage保存在CoreData中
- 19. 保存后CoreData - ManageObject无效
- 20. CoreData:关系:保存:查询
- 21. CoreData - 保存/加载帮助!
- 22. 保存coreData上的文字
- 23. 将CoreData保存到iCloud(Swift)
- 24. 将USSwitch保存到CoreData
- 25. Coredata关系,保存字典
- 26. CoreData删除保存错误
- 27. CoreData-apps中的“保存”
- 28. iPhone Coredata保存错误
- 29. CoreData具有自动保存保存一日一次,然后在下面的所有使用自动保存CoreData
- 30. 在CoreData中保存一对多关系
请把答案放在你的问题的实际答案,并接受它。好的开始在StackOverflow;) – Kametrixom
哦,是的,我忘了这一点。谢谢。 –
感谢您直接添加图片@Kametrixom –