如何在iOS中为HTML文件添加书签?

时间:2014-07-03 08:00:24

标签: html ios xcode plist nsbundle

我有一堆本地HTML文件,我想在应用程序中创建书签功能。 当用户按下书签按钮时,它将数据保存在核心数据中。 怎么做?

1 个答案:

答案 0 :(得分:1)

书签功能通常只包括保存现有数据的链接。为什么需要Core Data来保存?如果您有多个书签,那将是有意义的。在这一点上,这似乎有点矫枉过正。但是,您可以使用MagicalRecord(https://github.com/magicalpanda/MagicalRecord)对CoreData进行繁重的工作。

 // create a bookmark record, you can also add title, image, timestamp, etc...
 NSDictionary *bookmarkRecord = @{"url":"http://www.igraczech.com"};

 // prepare dictionary for all the bookmark records you want to save
 NSMutableDictionary *records = [NSMutableDictionary new];
 [records addObject:bookmarkRecord forKey:@"Bookmark title"];

 // this saves the 'records' dictionary (immutable by default), size limit is about a megabyte or two
 [[NSUserDefaults standardUserDefaults] setObject:records forKey:@"bookmarks"];
 [[NSUserDefaults standardUserDefaults] synchronize];

 // this loads bookmarks into mutable dictionary so you can add/edit/remove/save another one safely
 id savedBookmarks = [[NSUserDefaults standardUserDefaults] objectForKey:@"bookmarks"];

 // here the bookmarks are stored, you should use ivar/property instead of this example
 NSMutableDictionary *bookmarks = [NSMutableDictionary new];

 if (savedBookmarks)
 {
     bookmarks = [bookmarks addContentsFromDictionary:savedBookmarks];
 }