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 –