【问题标题】:How to create the folder in ios如何在ios中创建文件夹
【发布时间】:2017-12-01 11:57:45
【问题描述】:

我正在使用 corodva 媒体插件,我需要将文件保存在单独的文件夹中。我有两个文件,一个用于 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;
}

【问题讨论】:

  • 你有什么问题?
  • 我想创建一个新文件夹,但我不知道怎么做,在上面的代码中。
  • 它不是重复的问题,我使用的是cordova媒体文件插件,它支持ios,我需要在其中创建目录

标签: ios cordova phonegap-plugins


【解决方案1】:

试试这是客观的 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];
}

【讨论】:

  • @感谢您的宝贵时间,请在我的代码中解释一下,我需要更改文件夹名称
  • 你能告诉我你想做什么吗?您需要创建新文件夹。
  • 是的,我想创建新文件夹,但我不知道在哪里更改我的上述代码
  • 根据你的文件扩展名返回文件路径。
  • 是的,它返回文件路径,我的问题是我不知道上面的代码是如何工作的,因为我不熟悉,我只想通过上面的代码创建文件夹
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多