-1

I am using corodva media plugin,In which i need to save file in separte folder. i have two file one for Android and another for iOS,I created the directory in Android its works fine,But i need to create new directory in ios,I dont know about ios coding, I had ios file inside of it i have some methods which i shown in below code,I need to create Folder in ios and store my file there kindly suggest.

 // 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;
}
kitty sarvaj
  • 517
  • 5
  • 10
  • 24

1 Answers1

2

Try this this is the objective C version

  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];
}
KKRocks
  • 8,222
  • 1
  • 18
  • 84
  • @thanks for your time,Could please explain in my code,where i need to change folderName – kitty sarvaj Jun 28 '17 at 09:54
  • can you say me that what you want to do ? you need to create new folder. – KKRocks Jun 28 '17 at 09:58
  • yes i want to create new folder,But i dont know where change my above code – kitty sarvaj Jun 28 '17 at 10:01
  • it is return file path of according to your file extension . – KKRocks Jun 28 '17 at 10:05
  • yes it return the file path,my Issue is i don't know how the above code works because i am unfamiliar with that,i just want to create folder through the above code – kitty sarvaj Jun 28 '17 at 10:11
  • you can append new folder name in docsPath and write it to NSFileManager. – KKRocks Jun 28 '17 at 10:13
  • NSString *newFolderPath = [NSString stringWithFormat:@"%@/%@", docsPath, @"FolderName"]; [[NSFileManager defaultManager] createDirectoryAtPath:newFolderPath withIntermediateDirectories:NO attributes:nil error:&error]; – KKRocks Jun 28 '17 at 10:14
  • if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]){} this condition use for check your folder exist or not . if not exist folder then create new with this [[NSFileManager defaultManager] createDirectoryAtPath:newFolderPath withIntermediateDirectories:NO attributes:nil error:&error] – KKRocks Jun 28 '17 at 11:53
  • Its plugin which i am using in my app,https://github.com/Nagaraj007/Contacts-C-/blob/master/IosMedia, its a github which consist of all methods which is written for Ios, kindly suggest me if possible, i just want to create folder – kitty sarvaj Jun 28 '17 at 12:00
  • Consider that `stringByAppendingPathComponent` handles all path separators (`/`) automatically. – vadian Jun 28 '17 at 12:37