iOS:无法将文件保存到“应用程序支持"文件夹,但可以保存到“文档" [英] iOS: Can't save file to 'Application Support' folder, but can to 'Documents'

查看:30
本文介绍了iOS:无法将文件保存到“应用程序支持"文件夹,但可以保存到“文档"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以完全使用自定义名称下载二进制文件并将其保存到文档"文件夹中.

I can download and save a binary file to the 'Documents' folder with a custom name perfectly fine.

如果我只是将 URL 更改为Application Support"文件夹而不是Documents"文件夹,则无法写入该 URL,表示它不存在.

If I just change the URL to the 'Application Support' folder instead of the 'Documents' folder, it fails to write to that URL saying it doesn't exist.

这是网址构造代码:

- ( NSURL * ) getSaveFolder
{
    NSURL * appSupportDir    = nil;
    NSURL * appDirectory     = nil;
    NSArray * possibleURLs   = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSAllDomainsMask];
    
    if ( [possibleURLs count] >= 1 )
    {
        appSupportDir = [possibleURLs objectAtIndex:0];
    }

    if ( appSupportDir != nil)
    {
        NSString * appBundleID = [[NSBundle mainBundle] bundleIdentifier];
        appDirectory           = [appSupportDir URLByAppendingPathComponent:appBundleID];
    }

    return appSupportDir;
}

这是保存代码:

- ( void ) writeOutDataToFile:( NSData * )data
{
    NSURL * finalURL = [self.rootPathURL URLByAppendingPathComponent:self.aFileName];

    [data writeToURL:finalURL atomically:YES];
}


如果我将 NSArray 更改为:


If I change the NSArray to:

NSArray * possibleURLs   = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];

那么它保存得很好.

我已阅读 Apple Docs on File 内容但无法解决此问题 - 我错过了什么?

I've read the Apple Docs on File stuff and can't fix this - what am I missing?

推荐答案

Documents 目录不同,Application Support 目录默认不存在于应用程序的沙箱中.您需要先创建它,然后才能使用它.

Unlike the Documents directory, the Application Support directory does not exist in the app's sandbox by default. You need to create it before you can use it.

获取目录引用的更简单的方法是:

And a much simpler way to get a reference to the directory is:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *appSupportDirectory = paths.firstObject;

这篇关于iOS:无法将文件保存到“应用程序支持"文件夹,但可以保存到“文档"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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