Timak Timak - 1 month ago 16
Swift Question

Notification extension access Core Data

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).

Answer

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.

Comments