Search code examples
objective-cplistbookmarks

How to write and read data from plist to tableview


Hi I am trying to make bookmarks for my browser. Title of Page, Url of Page, any Comments related to page.

I tried to save it in plist, but unsuccessful. Can anyone can help me to save these thing to plist and retrive in table view. So, when user tap on title it will open url in UIWebView.

Here is the code I have tried, so far:

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"bookmarks.plist"]; 

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path])
{
      NSString *bundle = [[NSBundle mainBundle] pathForResource:@"bookmarks" ofType:@"plist"];
        [fileManager copyItemAtPath:bundle toPath: path error:&error]; //6
}
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

NSString *writeUrl = @"Page URL One";
NSString *writeTitle = @"Page Title One";
NSString *writeComment = @"Page comments";

[data setObject:[NSString stringWithString:writeUrl] forKey:@"url"];
[data setObject:[NSString stringWithString:writeTitle] forKey:@"title"];
[data setObject:[NSString stringWithString:writeComment] forKey:@"comment"];
[data writeToFile: path atomically:YES];

NSMutableDictionary *savedUrl = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

NSString *value;
value = [[savedUrl objectForKey:@"url"] stringValue];
NSLog(@"%@", value);

UPDATE: I successfully saved and retrieved data to and from plist. Issue is comming in this line

value = [[savedUrl objectForKey:@"url"] stringValue];

By removing stringValue solve the problem.

 value = [savedUrl objectForKey:@"url"];

Now My second issue. I make three items named url, title, comment, types String in plist file. How can i store different urls. Like

name website: inforains url: inforains.com title: Info Rains comment: good website articles

name website: hitechnology url: hitechnology.com title: Hitechnology comment: hmmm

ans soo on..

how can I store data like this.. so all name website will show on tableview and when user click on anyone, data related to that website will show. I hope i clear my question.


Solution

  • Change this line

    value = [[savedStock objectForKey:@"url"] stringValue];
    

    to

    value = [savedUrl objectForKey:@"url"];
    

    because you are saving NSString in dictionary... not NSNumber.

    Second issue:

    //create mutable array to save all objects
    NSMutableArray *objectsArray = [data objectForKey:@"objectsArray"];
    if(!objectsArray) {
        objectsArray = [[NSMutableArray alloc] init];
    }
    
    //create and add dictionary into array
    NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"url1", @"title1", @"comment2",nil] forKeys:[NSArray arrayWithObjects:@"url", @"title", @"comment",nil]];
    
    [objectsArray addObject:dictionary];
    
    [data setObject:objectsArray forKey:@"objectsArray"];
    
    [data writeToFile: path atomically:YES];
    
    
    // to read data
    NSMutableDictionary *savedUrl = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
    NSMutableArray *objectsArraySAved = [savedUrl objectForKey:@"objectsArray"];
    for (NSMutableDictionary *dic in objectsArraySAved) {
        NSLog(@"URL %@", [dic valueForKey:@"url"]);
    }