如何在iPhone设备中保存用户最喜欢的文章

发布于 2024-12-10 05:07:27 字数 91 浏览 6 评论 0原文

我有新闻门户应用程序,我希望用户在按下按钮时将当前正在阅读的整篇文章保存到 iPhone 设备,以便他可以随时访问它。我还想知道一篇文章是否保存到用户列表而不是保存两次

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟火散人牵绊 2024-12-17 05:07:27

您需要使用 NSUserDefaults

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

prefsObject 是您为保存您的最爱而编写的类。它可能只保存数组,但您可以在其中放置用于搜索、添加、删除等的便利函数。

urlString 是一个包含文章 URL 的 NSString。

addStringToURL 是将 urlString 添加到 NSMutableArray 的方法。

getNSArrayOfURLS 是一个返回包含所有 URL 的 NSMutableArray 的方法。

后面加载数据的

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

initWithNSArray是用NSArray加载NSMutableArray的方法,使用NSArray的方法mutableCopy,返回一个NSMutableArray。该方法可能看起来像

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

请注意,我尚未测试这个确切的代码,但我在我的应用程序中使用了类似的版本。

您可以在此处找到在 NSMutableArray 中搜索现有 URL 的解决方案< /a>.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文