通知扩展访问核心数据 [英] Notification extension access Core Data

查看:59
本文介绍了通知扩展访问核心数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用自定义UI发送本地通知,该通知向用户显示的内容比通知本身包含的内容更多。我想从应用程序的核心数据中获取内容。可能吗?当我调试通知内容扩展时,它在获取核心数据模型路径的以下行崩溃:

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

此外,当我 print(NSBundle .allBundles()),它仅显示与扩展名相关的代码(... / PlugIns / Notification Content.appex>(已加载))

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

推荐答案

将您的核心数据模型添加到目标中。在右侧的检查器中,当您打开模型时,请同时为您的扩展启用它。

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.

这篇关于通知扩展访问核心数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆