Tuesday, 23 September 2014

String file read/write in plist

First you create one plist file. Here lock is a plist file, unlocked is a string name. ulocked string create inside plist file.

Write:

    NSString *locat = [[NSBundle mainBundle]pathForResource:@"Plist_filename" ofType:@"plist"];
    NSMutableDictionary *un_lock = [[NSMutableDictionary alloc]initWithContentsOfFile:locat];
    [un_lock setObject:value forKey:@"plist_keyname"];

    [un_lock writeToFile:locat atomically:YES];

(OR)

          NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory ,NSUserDomainMask, YES);
              NSString *documentsDirectory = [sysPaths objectAtIndex:0];
              NSString *filePath =  [documentsDirectory stringByAppendingPathComponent:@"lock.plist"];
              NSMutableDictionary *plistDict; // needs to be mutable
              if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
                  plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
              } else {
                  // Doesn't exist, start with an empty dictionary
                  plistDict = [[NSMutableDictionary alloc] init];
              }
    
              [plistDict setObject:@"1"forKey:@"unlocked"];

              [plistDict writeToFile:filePath atomically:YES];


Read: 

   NSString *locat = [[NSBundle mainBundle]pathForResource:@"plist_filename" ofType:@"plist"];
    NSDictionary *un_lock = [[NSDictionary alloc]initWithContentsOfFile:locat];
    NSString *str = [un_lock objectForKey:@"plist_keyname"];

(OR)


   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString * path =[[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:@"lock.plist"]];
    NSDictionary *savedStock = [[NSDictionary alloc] initWithContentsOfFile: path];

    NSString *str = [savedStock objectForKey:@"unlocked"];

No comments:

Post a Comment