ios 8:捆绑路径更改 [英] ios 8: Bundle path changes

查看:61
本文介绍了ios 8:捆绑路径更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iOS应用程序,用于存储数据库和生成的html文档中文件的绝对路径。我刚刚将我的iPhone更新到iOS 8,现在当我运行应用程序时,似乎应用程序安装在每个重新编译的不同目录中。例如,在第一次构建/运行[[NSBundle mainBundle] bundlePath]上,在下一次构建/运行时返回不同的内容。到底是怎么回事?这是Apple的新功能吗?

I have an iOS app that stores the absolute path of files in a database and in generated html documents. I just recently updated my iPhone to iOS 8 and now when ever I run the app it seems that the app is installed in a different directory every re-compile. For example on the first build/run [[NSBundle mainBundle] bundlePath] returns something different on the next build/run. What is going on? Is this a new feature of Apple?

更新:创建了错误报告

代码示例:

如果我在多次构建/运行中运行以下行,那么每次都会得到不同的结果。

If I run the following line over multiple build/runs then I will get a different result each time.

#define kOLD_PATH @"oldPath"
NSString* newPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
NSString* oldPath = [[NSUserDefaults standardUserDefaults] objectForKey:kOLD_PATH];

NSLog(@"New Path: %@", newPath);
NSLog(@"Old Path: %@", oldPath);
NSLog(@"Result: %@", [oldPath isEqualToString:newPath] ? @"Same" : @"Changed");

[[NSUserDefaults standardUserDefaults] setObject:newPath forKey:kOLD_PATH];
[[NSUserDefaults standardUserDefaults] synchronize];

多次运行时输出如下所示

The output looks like this over multiple runs

New Path: /var/mobile/Containers/Data/Application/4FFCE2CB-580D-409A-90CB-EF2B8A1FB653/Library
Old Path: /var/mobile/Containers/Data/Application/B038B2DA-F85D-4E18-A5F1-8635834EC454/Library
Result: Changed

完全披露:在我的应用中,用户导入具有资源的网页(ePub)。资源与网页一起存储。该网页还访问属于应用程序包的资源。为了在加载网页时实现这一点,将基本URL设置为网页所在的目录,并通过绝对文件路径访问包资源。现在,每次更新时文件路径都会发生变化。我尝试创建捆绑资源的符号链接,但这也无法进行后续更新。

Full Disclosure: In my app the user imports a web page (ePub) that has resources. The resources are stored with the web page. The web page also accesses resources that are part of the app bundle. To achieve this when I load the web page the base url is set to the directory the web page is in and the bundle resources are accessed via absolute file paths. Now that file paths change on every update this is broken. I tried creating symbolic links to the bundle resources but that also fails un subsequent updates.

推荐答案

请参阅Apple的技术说明2406

突破性变化


从iOS 8开始,Documents和Library目录不再是应用程序包的兄弟姐妹

Beginning in iOS 8, the Documents and Library directories are no longer siblings of your application's bundle.

不要将完整路径/ URL存储到您的文档中。存储文件名并始终使用推荐的方法生成完整路径/ URL。

Don't store full path/URL to your documents. Store the file name and always generate full path/URL with recommended approach.

获取DocumentsDirectory URL

Get the DocumentsDirectory URL

// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

然后你从url获取路径并附加文件名以生成完整路径。

Then you get path out of url and append the file name to generate full path.

这篇关于ios 8:捆绑路径更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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