Notification extension access Core Data

↘锁芯ラ 提交于 2019-12-10 17:55:27

问题


I am trying to send a local notification with custom UI that shows the user more content than the notification itself contains. I want to get the content from the app's core data. Is it possible? When I debug the notification content extension it crashes on the following line where the core data model path is retrieved:

let modelURL = NSBundle.mainBundle().URLForResource("Model", withExtension: "momd")!

Also, when I print(NSBundle.allBundles()) from the notification extension code, it prints only the one related to the extension (.../PlugIns/Notification Content.appex> (loaded))

So is there a way to access core data in a notification content extension? I also wanted to try to use the notification service extension, but looks like it can be used only on push notifications (not local).


回答1:


Add your Core Data model to your target. In the inspector on the right, when you have your model open, enable it for your extension as well.

If you need to access the same store file, make sure to save it in a data container, shared between your app and extensions.



来源:https://stackoverflow.com/questions/40181369/notification-extension-access-core-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!