How to save in iphone device user's favorite articles

188 views Asked by At

I have newsportal application and i want user when hitting a button to save the whole article that is reading at the moment, to the iphone device so that he can access it whenever he wants. I also want to know if an article is save to the users list and not to save it twice

1

There are 1 answers

0
magmastonealex On BEST ANSWER

You will want to save using NSUserDefaults.

[prefsObject addStringToURL: urlString];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:[prefsObject getNSArrayOfURLS] forKey:@"FavouriteURL"];
[userDefaults synchronise];
[userDefaults release];

prefsObject is a class you wrote to hold your favourites. It might only hold the array, but you can put convenience functions for searching, adding, removing, etc. in there.

urlString is an NSString containing the URL to the article.

addStringToURL is a method that adds the urlString to an NSMutableArray.

getNSArrayOfURLS is a method to return the NSMutableArray with all of the URLs.

Later, to load the data

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[prefsObject initWithNSArray:(NSArray)[userDefaults objectForKey:@"FavouriteURL"]];

initWithNSArray is a method to load the NSMutableArray with an NSArray, using the NSArray method mutableCopy, which returns an NSMutableArray. That method might look like

-(void)initWithNSArray:(NSArray*)arrayToLoad{
prefsArray = [arrayToLoad mutableCopy];
}

Note that I have not tested this exact code, but I used a similar version in my app.

You can find a solution of searching the NSMutableArray for existing URLS here.