Re: Saving application data in ~/Library/Application Support/
Re: Saving application data in ~/Library/Application Support/
- Subject: Re: Saving application data in ~/Library/Application Support/
- From: Graham Cox <email@hidden>
- Date: Thu, 5 Feb 2009 13:26:01 +1100
On 5 Feb 2009, at 1:05 pm, Josh de Lioncourt wrote:
Hi,
I have some products under development written in C++. I am not
using Cocoa for the most part, as these are cross-platform projects.
The apps themselves need to save, store, and access information,
such as registration keys, user preferences, etc. It seems to me
that the logical place to do this is in the ~/Library/Application
Support/ directory.
Is it safe/recommended to access this directory using the above
format, or is there a more accepted shortcut designation for that
particular dir?
Usually you'd create a folder in the support folder for your app,
rather than place files directly at that level. It's also prudent to
use the FindFolder method rather than hard-coding the path. Here are a
couple of methods I use as part of a category extending
NSFolderManager. The last one creates a subfolder using the bundle
identifier - in fact most apps don't do that and advice I've received
on this list suggests using the app's name is more usual.
hth,
-Graham
- (NSString*) pathToFolderOfType:(const OSType) folderType
shouldCreateFolder:(BOOL) create
{
OSErr err;
FSRef ref;
NSString* path = nil;
err = FSFindFolder( kUserDomain, folderType, create, &ref);
if ( err == noErr )
{
// convert to CFURL and thence to path
CFURLRef url = CFURLCreateFromFSRef( kCFAllocatorSystemDefault,
&ref );
path = (NSString*) CFURLCopyFileSystemPath( url,
kCFURLPOSIXPathStyle );
CFRelease( url );
}
return [path autorelease];
}
- (NSString*) applicationSupportFolder
{
// returns the path to the general app support folder for the current
user
return [self pathToFolderOfType:kApplicationSupportFolderType
shouldCreateFolder:YES];
}
- (NSString*) thisApplicationsSupportFolder
{
// returns a path to a folder within the applicaiton support folder
having the same name as the app
// itself. This is a good place to place support files.
NSString* appname = [[NSBundle mainBundle] bundleIdentifier];
NSString* path = [[self applicationSupportFolder]
stringByAppendingPathComponent:appname];
// create this folder if it doesn't exist
BOOL result = NO;
if (![self fileExistsAtPath:path])
{
result = [self createDirectoryAtPath:path attributes:nil];
if( !result )
[NSException raise:NSGenericException format:@"The folder '%@'
could not be created", path ];
}
return path;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden