简体   繁体   中英

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. Into the app terget I can retrieve the persistent store path using the following method:

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

How can I find the persistent store path of the iOS app in the watchkit extesion target ?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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