如何在ios中创建文件夹

时间:2017-06-28 08:14:28

标签: ios cordova phonegap-plugins

我正在使用corodva媒体插件,其中我需要将文件保存在separte文件夹中。我有两个文件一个用于Android,另一个用于iOS,我在Android中创建了它的工作正常,但我需要在ios中创建新目录,我不知道ios编码,我有ios文件里面我有一些方法我在下面的代码中显示,我需要在ios中创建文件夹,然后在那里存储我的文件。

 // Maps a url for a resource path for playing
 // "Naked" resource paths are assumed to be from the www folder as its base
-(NSURL*)urlForPlaying:(NSString*)resourcePath
{
NSURL* resourceURL = nil;
NSString* filePath = nil;

// first try to find HTTP:// or Documents:// resources

if ([resourcePath hasPrefix:HTTP_SCHEME_PREFIX] || [resourcePath hasPrefix:HTTPS_SCHEME_PREFIX]) {
    // if it is a http url, use it
    NSLog(@"Will use resource '%@' from the Internet.", resourcePath);
    resourceURL = [NSURL URLWithString:resourcePath];
} else if ([resourcePath hasPrefix:DOCUMENTS_SCHEME_PREFIX]) {
    NSString* docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    filePath = [resourcePath stringByReplacingOccurrencesOfString:DOCUMENTS_SCHEME_PREFIX withString:[NSString stringWithFormat:@"%@/", docsPath]];
    NSLog(@"Will use resource '%@' from the documents folder with path = %@", resourcePath, filePath);
} else if ([resourcePath hasPrefix:CDVFILE_PREFIX]) {
    CDVFile *filePlugin = [self.commandDelegate getCommandInstance:@"File"];
    CDVFilesystemURL *url = [CDVFilesystemURL fileSystemURLWithString:resourcePath];
    filePath = [filePlugin filesystemPathForURL:url];
    if (filePath == nil) {
        resourceURL = [NSURL URLWithString:resourcePath];
    }
} else {
    // attempt to find file path in www directory or LocalFileSystem.TEMPORARY directory
    filePath = [self.commandDelegate pathForResource:resourcePath];
    if (filePath == nil) {
        // see if this exists in the documents/temp directory from a previous recording
        NSString* testPath = [NSString stringWithFormat:@"%@/%@", [NSTemporaryDirectory()stringByStandardizingPath], resourcePath];
        if ([[NSFileManager defaultManager] fileExistsAtPath:testPath]) {
            // inefficient as existence will be checked again below but only way to determine if file exists from previous recording
            filePath = testPath;
            NSLog(@"Will attempt to use file resource from LocalFileSystem.TEMPORARY directory");
        } else {
            // attempt to use path provided
            filePath = resourcePath;
            NSLog(@"Will attempt to use file resource '%@'", filePath);
        }
    } else {
        NSLog(@"Found resource '%@' in the web folder.", filePath);
    }
}
// if the resourcePath resolved to a file path, check that file exists
if (filePath != nil) {
    // create resourceURL
    resourceURL = [NSURL fileURLWithPath:filePath];
    // try to access file
    NSFileManager* fMgr = [NSFileManager defaultManager];
    if (![fMgr fileExistsAtPath:filePath]) {
        resourceURL = nil;
        NSLog(@"Unknown resource '%@'", resourcePath);
    }
}

 return resourceURL;
}

1 个答案:

答案 0 :(得分:2)

试试这是目标C版

  NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; //path for documents folder
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/FolderName"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]){
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
}