简体   繁体   中英

NSFileManager UUID app directory

I'm using NSFileManager and i'm trying to copy a file to path "/var/mobile/Applications/7AC2295E-2775-41EA-B017-AB4048A09F0C/Document" the file will copy fine.

but the path of "7AC2295E-2775-41EA-B017-AB4048A09F0C" is randomly changed in every time i delete and install the app again. So, is there a way to get the correct path of my app or search for file name, If file exist then replace\\delete..etc the file? thanks alot.

You don't have to use the absolute path, use something like

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
[[NSFileManager defaultManager] copyItemAtPath:[[NSBundle mainBundle]pathForResource:@"test" ofType:@"txt"] toPath:[documentsDirectory stringByAppendingPathComponent:@"test.txt"] error:nil];

This demonstrates copying the file test.txt from the apps bundle to the documents directory. Do something like this to create a file test.txt

[[NSFileManager defaultManager] createFileAtPath:[documentsDirectory stringByAppendingPathComponent:@"test.txt"] contents:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://a-cstudios.com/text.json"]] attributes:nil];

EDIT

If your using this and want to use it on a jailbroken device, to put something in the sandbox is the same as I said before, so you still don't have to use the UUID. If you want to put something in a system directory, that is when you would use something like /usr/include/ , or wherever you want to. To put something in another apps sandbox, you would have to use the complete path, and you would have to know the UUID, as thats not part of your sandbox. So, unless you're copying a file to an app that you made, you have to use the complete path.

The proper way to get access to the Documents directory in your app's sandbox is:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = paths[0];

Then you create your path:

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"filename.ext"];

If your application needs to write a file to its Documents directory.
use something like :

NSString  *filePath = NSHomeDirectory();

then append your actual file name to filePath.
then use filePath in your FileManager calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM