Question

I have created application that record sound and display that sound file in tableview. In that application when sound is recorded than it's filepath will store in a array , which array i use to populate my tableview. But my problem is that when i close the application then my array will be empty , and i lost my recorded file.

So now how can i get my recorded file when i open my app second time. I am storing sound file in document directory.

Was it helpful?

Solution

Store array in NSUserDefaults. Retrive and Store the array in NSUserDefaults .. see the example of NSUserDefaults

In AppDelegate.h file just declare variable...

    NSUserDefaults  *userDefaults;
    NSMutableArray *yourArray;
    after...

In AppDelegate.m Fille in applicationDidFinishLonching: Method

    userDefaults = [NSUserDefaults standardUserDefaults];
    NSData *dataRepresentingtblArrayForSearch = [userDefaults objectForKey:@"yourArray"];
    if (dataRepresentingtblArrayForSearch != nil) {
        NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingtblArrayForSearch];
        if (oldSavedArray != nil)
            yourArray = [[NSMutableArray alloc] initWithArray:oldSavedArray];
        else
            yourArray = [[NSMutableArray alloc] init];
    } else {
        yourArray = [[NSMutableArray alloc] init];
    }
    [yourArray retain];

after that when you want to insert Data in this UserDefaults Use Bellow Code...

    [appDelegate.yourArray addObject:yourDataString];///set your value or anything which you want to store here...
    NSData *data=[NSKeyedArchiver archivedDataWithRootObject:appDelegate.yourArray];
    [appDelegate.userDefaults setObject:data forKey:@"yourArray"];
    [appDelegate.userDefaults synchronize];

i hope this help you...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top