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.