开发者

How to save in iphone device user's favorite articles

开发者 https://www.devze.com 2023-04-12 21:55 出处:网络
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 wan

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 user开发者_如何转开发s list and not to save it twice


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号