简体   繁体   English

如何在WatchKit Extension目标中获取核心数据持久存储路径

[英]How to get the core data persistent store path in WatchKit Extension target

I developed a iOS app that uses Core Data framework and saves the persistent store in the application document directory. 我开发了一个使用Core Data框架的iOS应用程序,并将持久存储保存在应用程序文档目录中。 Into the app terget I can retrieve the persistent store path using the following method: 在app terget中,我可以使用以下方法检索持久性存储路径:

- (NSString *)applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

Then I created a new WatchKit extension target and I want to retrieve the persistent store saved by the iOS app, but when I try to use the above method in the Interface Controller of the watch extension, it returns a different path . 然后我创建了一个新的WatchKit扩展目标,我想检索iOS应用程序保存的持久存储, 但是当我尝试在watch扩展的Interface Controller中使用上述方法时,它返回一个不同的路径

How can I find the persistent store path of the iOS app in the watchkit extesion target ? 如何在watchkit extesion目标中找到iOS应用程序的持久存储路径?

Your WatchKit Extension would not have permissions to access the Core Data store in your app's Documents directory, because that is outside of the extension's sandbox. 您的WatchKit扩展程序无权访问应用程序的Documents目录中的Core Data存储区,因为它位于扩展程序的沙箱之外。

If you want your extension to be able to access the data store, then you need to put it in a place that both the app and the extension can access. 如果您希望扩展程序能够访问数据存储,则需要将其放在应用程序和扩展程序都可以访问的位置。 I recommend reading up on App Group Containers (the "Sharing Data with Your Containing App" section), which were added for this very purpose of sharing files between related apps and extensions. 我建议阅读App Group Containers (“与您的应用程序共享数据”部分),这是为了在相关应用程序和扩展之间共享文件而添加的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM