Reputation: 2084
I'm wondering if this snippet of code returns a directory that is private to an application and protected by the application sandbox.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
return [paths firstObject];
To test this myself, I wrote two applications. Application prateek.writer
runs the following piece of code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *applicationSupportDirectory = [paths firstObject];
NSLog(@"applicationSupportDirectory: '%@'", applicationSupportDirectory); // This is used by the second app.
NSString *filePath = [applicationSupportDirectory stringByAppendingPathComponent:@"myfile.txt"];
if (![[NSFileManager defaultManager] fileExistsAtPath:applicationSupportDirectory
isDirectory:NULL]) {
NSError *error = nil;
if (![[NSFileManager defaultManager] createDirectoryAtPath:applicationSupportDirectory
withIntermediateDirectories:YES
attributes:nil
error:&error]) {
NSLog(@"error %@", error);
}
}
[@"Hello World" writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
The second application, prateek.reader
, runs the following piece of code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
// This string is copied from the logs of the writer application.
NSString *applicationSupportDirectory = @"/Users/prateek/Library/Developer/CoreSimulator/Devices/218F1365-5AF2-4003-83AF-6337E0EA8207/data/Containers/Data/Application/B8918247-5509-458D-AB9E-0F1273EAD2FE/Library/Application Support";
NSString *filePath = [applicationSupportDirectory stringByAppendingPathComponent:@"myfile.txt"];
NSString *string = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"Read file with contents: %@", string);
My reader app was able to read the data from the writer app. I ran this on an iPhone 8 Plus and iPad Air simulator and saw the same results. Could it maybe have something to do with this being simulator, or the fact that these are apps created/signed by the same developer (me in this case).
Upvotes: 0
Views: 1412
Reputation: 318794
The Application Support folder is specific to each app and is within an app's private sandbox. No app can access this folder of another app, when run on a real iOS device.
The simulator has no such limitations. It's one of the many differences between running on a real device and a simulator.
Upvotes: 1