Wednesday, August 8, 2012

Handling NSDocumentDirectory of your app to store plist files

Normally, the plist files in Resource folder of iOS projects are readable only , that means those plist file can not be modified or we can not update it. So, to over come this problem , to set the read/write property to the plist we need to save/create them in the documents directory  of app.

Following code shows  how to  create plist file  in device's documents directory :

1. Creating plist file in Documents Directory
    1.1 Copying plist file from project resource folder to documents directory
   // this assumes you have plist file created manually in your project resource folder and here we are simply copying it from resource bundle to documents directory
  
   NSError *error;
// returning the path array of documents directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   NSUserDomainMask, YES);   
//  returning the path at index 0
   NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"testFile.plist"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
// checking plist file already exists or not , if not then copy it from resource folder to documents directory
    if (![fileManager fileExistsAtPath: path]) 
    {
        NSString *bundle = [[NSBundle mainBundle]pathForResource:@"testFile" ofType:@"plist"];
        [fileManager copyItemAtPath:bundle toPath: path error:&error];
    }

     1.2 Creating plist file in documents directory programmatically

        NSError *error;
      // returning the path array of documents directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   NSUserDomainMask, YES);   
     //  returning the path at index 0
   NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"testFile.plist"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
            if (![fileManager fileExistsAtPath: path]){
               path = [documentsDirectory stringByAppendingPathComponent:@"testFile.plist"];
            }
            
            NSMutableDictionary *data;
            if ([fileManager fileExistsAtPath: path])
            {
                data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
            }
            else
            {
                // If the file doesn’t exist, create an empty dictionary or array as per your requirement
                data = [[NSMutableDictionary alloc] init];
            }
            
            [data setObject:regionDict forKey:regionName];
            [data writeToFile:path atomically:NO];

2. Reading the plist file:
 
    NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
//  tempDict : assuming plist file is of type dictionary (other cases : Array,String)

2. Updating the plist file:

      [tempDict setObject:@"Your object" forKey:@"Dict.Key"];
      //Here, @"Your object" may be Array, dictionary  or simply strings as per your requirements
     // finally saving new updates
    [tempDict writeToFile:path atomically:NO];

1 comment: