Question

In my app I need to store the data in a plist file to use in another UIViewController and to keep a tiny db on the app. I made so:

  1. I get the information that I need to store in this file
  2. I inserted this information in an NSDictionary
  3. I inserted the NSDictionary in an NSMutableArray
  4. Now I made an utility class that handle the work with file But it doesn't create the plist file in the Documents directory. Here's my code:

ViewController.m

- (void)saveActivity {
    if ([FileHander plistExist]) {
        NSMutableArray *dataFromFile = [[NSMutableArray alloc]init];
        dataFromFile = [FileHander loadDataFromFile];
        if (![[dataFromFile lastObject]objectForKey:@"type"]) {
            [FileHander saveFileWithArray:arrayActivity];
        } else {
            [dataFromFile addObject:arrayActivity];
            [FileHander saveFileWithArray:dataFromFile];
        }
    } else {
        NSLog(@"%@", arrayActivity);
        [FileHander saveFileWithArray:arrayActivity];
    }
}

the arrayActivity contains this (coordinate, speed, time etc...):

(
        {
        coordinates =         (
            "<+45.44083526,+9.19919774> +/- 10.00m (speed 0.00 mps / course 71.02) @ 04/02/14 12:46:08 Ora standard dell'Europa centrale",
            "<+45.44083561,+9.19919755> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
            "<+45.44083612,+9.19919728> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
            "<+45.44083663,+9.19919700> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
            "<+45.44083714,+9.19919673> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
            "<+45.44084181,+9.19917132> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
            "<+45.44083100,+9.19919440> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
            "<+45.44088744,+9.19913454> +/- 50.00m (speed 0.00 mps / course -1.00) @ 04/02/14 12:46:35 Ora standard dell'Europa centrale",
            "<+45.44088744,+9.19913454> +/- 30.00m (speed 0.00 mps / course -1.00) @ 04/02/14 12:46:36 Ora standard dell'Europa centrale",
            "<+45.44088744,+9.19913454> +/- 30.00m (speed 0.00 mps / course -1.00) @ 04/02/14 12:46:37 Ora standard dell'Europa centrale",
            "<+45.44088744,+9.19913454> +/- 30.00m (speed 0.00 mps / course -1.00) @ 04/02/14 12:46:39 Ora standard dell'Europa centrale"
        );
        speed =         (
        );
        steps = 16;
        time = "00:00:08";
        type = Trekking;
    }
)

the utility class is this:

FileHandler.m

#import "FileHander.h"

@implementation FileHander

+ (BOOL) plistExist {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *path = [documentsDir stringByAppendingPathComponent:@"activity.plist"];
    return [[NSFileManager defaultManager]fileExistsAtPath:path];
}

+ (NSMutableArray *) loadDataFromFile {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *path = [documentsDir stringByAppendingPathComponent:@"activity.plist"];
    NSMutableArray *dataFound = [NSArray arrayWithContentsOfFile:path];
    return dataFound;
}

+ (BOOL) deletePlist {
    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"activity.plist"];
    NSError *error;
    BOOL success =[manager removeItemAtPath:filePath error:&error];
    if (success) {
        return YES;
    }
    else
    {
        return NO;
    }
}

+ (void) saveFileWithArray: (NSMutableArray*)activityArray {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"activity.plist"];
    [activityArray writeToFile:fileName atomically:YES];
}

@end

When I execute this app it can't create the plist file on the device (I looked for this file by using the Mac App iExplorer), what's wrong in my code? I used the same code to develop another app and it works great. I hope you can help me to fix the issue. Thank you

Was it helpful?

Solution

Please try below code to create plist file.. I have tried it and works fine..

CREATE FILE WITH NSARRAY:

- (void) saveFileWithArray: (NSMutableArray*)activityArray {  
    NSData * data = [NSKeyedArchiver archivedDataWithRootObject:activityArray];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex:0];

    NSString *Path =[NSString stringWithFormat:@"%@/%@.plist",docDir,@"isLogin"];
    NSLog(@"-->%@",Path);        // PATH OF YOUR PLIST FILE
    BOOL didWriteSuccessfull = [data writeToFile:Path atomically:YES];
       if (didWriteSuccessfull) {
           NSLog(@"store succsessfully");
          }
     else {
           NSLog(@"Error in Storing");
    } 
}

GET NSARRAY FROM FILE:

- (NSMutableArray *) loadDataFromFile {
   NSArray *myArray=[[NSArray alloc]init];
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *docDir = [paths objectAtIndex:0];

   NSString *Path =[NSString stringWithFormat:@"%@/%@.plist",docDir,@"isLogin"];

   if ([[NSFileManager defaultManager] fileExistsAtPath:Path])
       {
          NSLog(@"exist");
          myArray=[[NSArray alloc]initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:[NSData dataWithContentsOfFile:Path]]];
          return [NSMutableArray arrayWithArray:myArray];
       }
        else{
            NSLog(@"not exist");
            return nil;
       }
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top