Reputation: 681
Does OS X have a place within the application to save files like how you save files to the documents folders in iOS. I have .plist files to save for my app but I only want to be able to access them form within the app. Don't want do have a folder in the users documents folder or anything.
Primarily an iOS guy so apologies if this is obvious.
Upvotes: 3
Views: 631
Reputation: 1691
This worked for me:
-(void) setupApp {
NSString * myAppPath = [[Configurations createApplicationDirectoryWithPathComponent:MY_APP_DIR_NAME] path];
self.plistPath = [myAppPath stringByAppendingString:CONFIG_FILE_NAME];
[self createNewConfigFileWithDictionary:yourDictionary];
}
-(void) createNewConfigFileWithDictionary:(NSDictionary*)dictionary {
@try {
NSFileManager * fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:self.plistPath]) {
[dictionary writeToFile:self.plistPath atomically:YES];
}
} @catch (NSException *exception) {
NSLog(@"[MyClass createNewConfigFileWithDictionary] Problem:%@ Description: %@", [exception name],[exception reason]);
} @finally {}
}
+ (NSURL*) createApplicationDirectoryWithPathComponent:(NSString*)path {
NSFileManager*fm = [NSFileManager defaultManager];
NSURL* dirPath = nil;
// Find the application support directory in the home directory.
NSArray* appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask];
if ([appSupportDir count] > 0) {
// Append the bundle ID to the URL for the
// Application Support directory
dirPath = [[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:path];
// If the directory does not exist, this method creates it.
// This method is only available in macOS 10.7 and iOS 5.0 or later.
NSError* theError = nil;
if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES attributes:nil error:&theError]) {
@throw [NSException exceptionWithName:@"CreateApplicationDirectoryWithPathComponentException"
reason:theError.localizedDescription
userInfo:nil];
return nil;
}
}
return dirPath;
}
Upvotes: 0
Reputation: 42588
I'm not exactly sure what you want here.
To get a directory to store files in an out of the way location, you can use the Application Support directory (~/Library/Application Support/[APP_NAME]/
).
To get the name of this directory, you pass NSApplicationSupportDirectory
to -URLsForDirectory:inDomains:
(see: Managing Files and Directories).
To prevent the user from reading a file, you can encrypt it (see: Encrypting and Hashing Data).
Upvotes: 3