My iOS app sends a database to
WatchKit (watchOS 2)
. In WatchKit
i use the following code to get the watch document folder to save the new file in:- (void)session:(WCSession * _Nonnull)session
didReceiveFile:(WCSessionFile * _Nonnull)file
{
NSURL* url = [file fileURL];
NSURL* fileURL = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:@"MyDatabase.sqlite"];
// This results in an URL:
// @"file:///var/mobile/Containers/Data/PluginKitPlugin/47E13085-9030-4ED2-906F-01CBA323F07A/Documents/MyDatabase.sqlite"
// However, when I call the function:
[[NSFileManager defaultManager] replaceItemAtURL:fileURL withItemAtURL:url backupItemName:nil options:0 resultingItemURL:nil error:&error];
}
It throws an error saying:
Error Domain=NSCocoaErrorDomain Code=512 "The file “MyDatabase.sqlite” couldn’t be saved in the folder “Documents”." Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “MyDatabase.sqlite”
How can I find a folder I'm allowed to save to? Naturally this code worked fine in the simulator so I assume I'm missing something.
BR Frederik