iOS*_*Fun 32 directory file save ios
我可以将二进制文件下载并保存到'Documents'文件夹中,并且自定义名称完全正常.
如果我只是将URL更改为"应用程序支持"文件夹而不是"文档"文件夹,则无法写入该URL,表明它不存在.
这是URL构造代码:
- ( 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更改为:
NSArray * possibleURLs   = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
然后它保存好.
我已经阅读了Apple Docs on File的东西,无法解决这个问题 - 我错过了什么?
rma*_*ddy 48
与Documents目录不同Application Support,默认情况下,该应用程序的沙箱中不存在该目录.您需要先创建它才能使用它.
获取目录引用的一种更简单的方法是:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *appSupportDirectory = paths.firstObject;
chr*_*ood 48
如果有人不确定如何做rmaddy所描述的:
NSString *appSupportDir = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject];
//If there isn't an App Support Directory yet ... 
if (![[NSFileManager defaultManager] fileExistsAtPath:appSupportDir isDirectory:NULL]) {
    NSError *error = nil;
//Create one 
    if (![[NSFileManager defaultManager] createDirectoryAtPath:appSupportDir withIntermediateDirectories:YES attributes:nil error:&error]) {
        NSLog(@"%@", error.localizedDescription);
    }
    else {
// *** OPTIONAL *** Mark the directory as excluded from iCloud backups 
        NSURL *url = [NSURL fileURLWithPath:appSupportDir];
        if (![url setResourceValue:@YES
                            forKey:NSURLIsExcludedFromBackupKey
                             error:&error])
        {
            NSLog(@"Error excluding %@ from backup %@", url.lastPathComponent, error.localizedDescription);
        }
        else {
            NSLog(@"Yay");
        }
    }
}
我遇到了同样的问题,并决定使用更简洁的方法:
let fileManager = NSFileManager.defaultManager()
let urls = fileManager.URLsForDirectory(.ApplicationSupportDirectory, inDomains: .UserDomainMask) as! [NSURL]
if let applicationSupportURL = urls.last {
    fileManager.createDirectoryAtURL(applicationSupportURL, withIntermediateDirectories: true, attributes: nil, error: nil)
}
这样做是因为createDirectoryAtURL使用withIntermediateDirectories: true如果不存在,只创建文件夹.
一个班轮 - 如有必要也将创建:
[[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil]
| 归档时间: | 
 | 
| 查看次数: | 14369 次 | 
| 最近记录: |