What is the hexadecimal part in the Documents folder's path?

百般思念 提交于 2019-12-23 02:48:10

问题


I have a file located in my app's Documents folder. When the app is terminated I save the file's URL in the AppDelegate's applicationWillTerminate method:

// archiver init code
[archiver encodeObject:file.URL forKey:kFileURL];
// finish encoding and write data to file system

But when trying to restore the file on the next app launch the file manager cannot locate the file: After calling

NSURL *fileURL = [unarchiver decodeObjectForKey:kFileURL];
NSString *filePath = fileURL.path;

the method

[[NSFileManager defaultManager] fileExists:filePath];

returns NO.

I tried to find the reason for this and I discovered that the path to the Documents folder changes with every app launch. The part that changes is the hexadecimal folder in the middle. Here are two examples:

/private/var/mobile/Applications/04083A4A-87AC-4E3C-8BA1-F002B97AE304/Documents/...
/private/var/mobile/Applications/65D136BA-42C3-887A-B947-7FE396978153/Documents/...

I always thought that the hexadecimal part is some sort of ID unique to every app. But as it changes: What exactly is that number? And how can I relocate my file then after terminating and relaunching my app?


回答1:


You should just get the directory for the document folder and then load your file.

+ (NSString *)documentDataPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    if ([paths count] == 0) {
        return nil;
    }
    NSString *directory = [paths objectAtIndex:0];
    if (directory == nil) {
        NSLog(@"NSDocumentDirectory not found!");
    }
    return directory;
}


来源:https://stackoverflow.com/questions/26305742/what-is-the-hexadecimal-part-in-the-documents-folders-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!