To locate the file inside of your Application bundle, you will need to use the mainBundle
. Assuming you've got a file foo.txt
that you need to locate, and it's being stored in the standard resource location, do the following:
NSBundle *appBundle = [NSBundle mainBundle];
NSString *srcPath = [appBundle pathForResource: @"foo" ofType: @"txt"];
at this point, you should have the path to your foo.txt
in srcPath.
If you are downloading the file for the user, you should place it into the user's Downloads directory. If you are downloading the file for internal use, it belongs in either /Library/Application Support/<yourapp>/...
or ~/Library/Application Support/<yourapp>...
.
The right way to find these is using NSSearchPathForDirectoriesInDomains
.
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDownloadsDirectory,
NSUserDomainMask, YES);
will yield an array (should be 1 element), with the current user's download directory path in it. If they're requesting the file, you should use this as the destination path.
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSApplicationSupportDirectory,
NSUserDomainMask, YES);
will yield an array (should be 1 element), with the current user's Application Support path in it. If this is a per-user download for the App's internal use, it should be stored in a sub-directory created by using your App name or identifier appended to this path.
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSApplicationSupportDirectory,
NSSystemDomainMask, YES);
will yield an array (should be 1 element), with the machine's Application Support path in it. If this is a machine-wide download for the App's internal use, it should be stored in a sub-directory created by using your App name or identifier appended to this path.
Once you've created your source and destination path, you'll want to use NSFileManager
to move the file into place. In the case of the application support directories, you'll need to first verify that the directory you want exists, and if it doesn't, create it (using -createDirectoryAtPath:withIntermediateDirectories:attributes:error
).
NSFileManager *manager = [[NSFileManager alloc] init];
NSError *error;
if (![manager copyItemAtPath: srcPath toPath: dstPath error: &error]) {
NSLog("Error copying %@ to %@, %@", srcPath, dstPath, error);
}
[manager release];