Documents 文件夹路径中的十六进制部分是什么? [英] What is the hexadecimal part in the Documents folder's path?

查看:95
本文介绍了Documents 文件夹路径中的十六进制部分是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的 Documents 文件夹中有一个文件.当应用程序终止时,我将文件的 URL 保存在 AppDelegate 的 applicationWillTerminate 方法中:

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;

方法

[[NSFileManager defaultManager] fileExists:filePath];

返回NO.

我试图找出造成这种情况的原因,我发现 Documents 文件夹的路径随着每次应用程序启动而变化.变化的部分是中间的十六进制文件夹.这里有两个例子:

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/...

我一直认为十六进制部分是每个应用程序独有的某种 ID.但随着它的变化:这个数字到底是多少?以及如何在终止并重新启动我的应用后重新定位我的文件?

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?

推荐答案

您应该只获取文档文件夹的目录,然后加载您的文件.

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;
}

这篇关于Documents 文件夹路径中的十六进制部分是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆