0

In my Application i save Recording file in Document Directory.then later gets all these file from document directory and showing all these files in My mainClass where i have UITableview for it. When i Click on any row of this mainClass Tableview it goes to next Class where i play this file,delete this file and Send this file to my Favourite Recording Class where i have Tableview for it.Now my play button action method and delete Button action method works fine but i Don't know how to send this file to my Favurite Class Tableview.now i show here my delete button Code through which we can get the basic idea.

  -(IBAction)deleteFile
 {
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDir = [dirPaths objectAtIndex:0];
    NSString *documentPath = [docsDir stringByAppendingPathComponent:@"MyRecordings"];
    NSString *soundFilePath = [documentPath stringByAppendingPathComponent:fileToPlay];
    recordedTmpFile = [NSURL fileURLWithPath:soundFilePath];
    [[NSFileManager defaultManager] removeItemAtURL:recordedTmpFile error:nil];

}

As my Delete button Code show how we delete the Current Recording file which i selected from MainTableview .Now if the user Want to send the current Recording file to my Favourite Class Tableview instead of Deleting it then Whether i use the another Document folder here if answer is yes? then how we can save the Current file(recordedTmpFile) in this new Document folder.

 -(IBAction)AddToFavourite
{
NSArray *dirPaths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir1 = [dirPaths1 objectAtIndex:0];
NSString *documentPath1 = [docsDir1 stringByAppendingPathComponent:@"MyFovouriteRecordings"];

// How to pass here the Current Recording File (recordedTmpFile) to this new document Folder.
}

When i NSlog recordedTmpFile it show result :file://localhost/Users/Umar/Library/Application%20Support/iPhone%20Simulator/4.2/Applications/9219677A-B0E3-4B78-B2E5-FEA49D689618/Documents/MyRecordings/06:Dec:12_05:54:07%20PM+Active%20song

Any Help will be appriated.Thanks

NSCool
  • 229
  • 1
  • 12
  • 1
    May be the better approach rather than moving this file in favourite folder you should create a database for favourites and save the file path in database on AddToFavourite action, and whenever you want show favourites recordings fetch data from database and show it in tableview. – prasad Dec 13 '12 at 14:19
  • Thank @prasad for quick Response if we save the this current file to another Document folder and then getting data from this document in FavouritClass tableview.Beacuse i already use the Document folder for my MainTableview.Can u please give me any idea about this. – NSCool Dec 13 '12 at 14:27
  • you can use http://stackoverflow.com/questions/1762836/create-a-folder-inside-documents-folder-in-ios-apps to create favourites folder and to copy file use NSFileManager *fileManager = [NSFileManager defaultManager]; [fileManager copyItemAtPath:docPath toPath:favFloderPath error:&error]; – prasad Dec 13 '12 at 14:37
  • please post your Answer according to my requirments . i think this Method Will work for me. – NSCool Dec 13 '12 at 16:21

2 Answers2

1

Just create an instance of NSData with your original file. Than write it to the other location.

//write file to the other location
NSData *myData = [[NSData alloc] initWithContentsOfFile:originalPath];

[myData writeToFile:newDataPath options:NSDataWritingAtomic error:&error];

//delete file from original location
NSFileManager *fileManager = [[NSFileManager alloc] init];

[fileManager removeItemAtPath:originalPath error:&removeError];
Andrei Filip
  • 1,077
  • 14
  • 32
  • Please See my edit. whether i need to create another Document Folder using your Code.please edit your answer according to my Requirements that i explain above. – NSCool Dec 13 '12 at 16:20
1
-(IBAction)AddToFavourite {
    NSArray *dirPaths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   NSUserDomainMask, YES);
    NSString *docsDir1 = [dirPaths1 objectAtIndex:0];
    NSString *documentPath1 = [docsDir1 stringByAppendingPathComponent:@"MyFovouriteRecordings"];
    NSString *soundFilePath1 = [documentPath1 stringByAppendingPathComponent:fileToPlay];
    // How to pass here the Current Recording File (recordedTmpFile) to this new document Folder.

    // First check if the directory existst
    if([[NSFileManager defaultManager] fileExistsAtPath:documentPath1]==NO) {
        NSLog(@"Creating content directory: %@",documentPath1);
        NSError *error=nil;
        if([[NSFileManager defaultManager] createDirectoryAtPath:documentPath1 withIntermediateDirectories:NO attributes:nil error:&error]==NO) {
            NSLog(@"There was an error in creating the directory: %@",error);   
        }
    }

    //then get the origin file path
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDir = [dirPaths objectAtIndex:0];
    NSString *documentPath = [docsDir stringByAppendingPathComponent:@"MyRecordings"];
    NSString *soundFilePath = [documentPath stringByAppendingPathComponent:fileToPlay];

    //Then convert paths to URL
    NSURL* originURL = [NSURL fileURLWithPath:soundFilePath];
    NSURL* destinationURL = [NSURL fileURLWithPath:soundFilePath1];

    //Then, you have to copy the file
    [[NSFileManager defaultManager] copyItemAtURL:destinationURL toURL:originURL error:NULL];
}

That should do it, tell me if there is any error.

LuisEspinoza
  • 8,318
  • 5
  • 32
  • 55