簡體   English   中英

如何在iOS中創建文件夾

[英]How to create the folder in ios

我正在使用corodva媒體插件,在其中我需要將文件保存在separte文件夾中。 我有兩個文件,一個用於Android,另一個用於iOS,我在Android中創建了目錄,它的工作正常,但是我需要在ios中創建新目錄,我不知道ios編碼,我里面有ios文件,我有一些方法我在下面的代碼中顯示了該代碼,我需要在ios中創建Folder並將文件存儲在該文件夾中。

 // 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;
}

試試這個,這是目標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];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM