ios - 如何实现 JSON 的离线缓存?

标签 ios objective-c json afnetworking offline-caching

我正在构建类似 TechCrunch 的文章阅读应用。我正在从服务器获取 JSON 内容数据,并在 UITableView 中显示 JSON 内容,例如文章图片、标题和作者姓名。

当用户点击一篇文章时,它会在 UIWebView 中打开。

我已经通过已在 UITableView 中获取的 segue 传递了文章的标题、作者姓名和内容。

我在我的项目中集成了第三方库 AFNetworking。我想实现文章的离线缓存意味着如果没有互联网用户可以阅读文章。

这是我的代码:

表格 View .m

              - (void)viewDidLoad
             {
                [super viewDidLoad];
                tempJson = [[NSMutableArray alloc] init];
                [self.tableView reloadData];
                NSString *jsonLink=[NSString stringWithFormat:@“www.example.com&page=1"];
                NSURL *url = [[NSURL alloc] initWithString:jsonLink];
                NSURLRequest *request = [NSURLRequest requestWithURL:url];
                AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
                operation.responseSerializer = [AFJSONResponseSerializer serializer];
                [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
                 NSArray *jsonArray = (NSArray *)responseObject;
                 for (NSDictionary *dic in jsonArray) {
                Json *json = [[Json alloc] initWithDictionary:dic];
                [tempJson addObject:json];
                   }
                self.jsons = [[NSArray alloc] initWithArray:tempJson];
                [self.tableView reloadData];
               } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                             message:[error localizedDescription]
                                                   delegate:nil
                                                   cancelButtonTitle:@"Ok"
                                                   otherButtonTitles:nil];
                                                   [alertView show];

                                             }];
                                           [operation start];
                               [self.tableView addInfiniteScrollingWithActionHandler:^{
                                   }];
                                 }

                -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
                  {
                     if ([[segue identifier] isEqualToString:@"ShowDetails1"]) {
                     Webview *ysdetailviewcontroller = [segue destinationViewController];
                     NSIndexPath *myindexpath1 = [self.tableView indexPathForSelectedRow];
                     long row1 = [myindexpath1 row];
           dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                     ysdetailviewcontroller.DetailModal1 = @[[self.jsons[row1] title],          [self.jsons[row1] author],[self.jsons[row1] a_image],[self.jsons[row1] content], [self.jsons[row1] link],[self.jsons[row1] date]];
                         });
                       } 
                      }

网页 View .m

                - (void)viewDidLoad
                     {
                  [super viewDidLoad];
                  NSString *Title=[NSString stringWithFormat:@"<div style=\"line-height:80px;font-size:70px;font-family: Helvetica, Arial, Sans-Serif;font-weight:600;padding-left:40px; !important;padding-right:40px; !important;\">%@</div>",  _DetailModal1[0]];
                  NSString *AuthorName=[NSString stringWithFormat:@"<div><div style=\"color:#9E9E9E;line-height:70px;font-size:35px;font-family: Helvetica, Arial, Sans-Serif;font-weight:800;float:left;float:left;text-align:left;padding-left:40px; !important;\">%@&nbsp&nbsp</div>",_DetailModal1[1]];
                  NSString *Date=[NSString stringWithFormat:@"<div style=\"line-height:70px;font-size:35px;font-family: Helvetica, Arial, Sans-Serif;font-weight:200;\">%@</div></div><br><br>",_DetailModal1[5]];
                  NSString *url=[[NSString alloc] initWithString:_DetailModal1[3] ];//_DetailModal1[3] contains article content
                  NSString *javaScriptStar=@"<!DOCTYPE html><html><head><body></head><div style = \"line-height:70px !important;font-size:52px !important;font-family: Helvetica, Arial, Sans-Serif;font-weight:400;padding-left:40px; !important;padding-right:40px; !important;\">";
                  NSString *javaScriptEnd=@"</font></div></body></html>";
                  NSString *javaScriptLink=@"<body link=\"#3366CC\”>";  
                  NSString *javaScriptDec=@"<style type=\"text/css\">a {text-decoration: none; font-weight:bold; }img {  display: block;margin-left: auto; margin-right: auto;margin-top: 8px;margin-bottom: 8px; width : 800px !important;height:auto;}</style>";
                  NSString* myURLString = [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@",Title,AuthorName,Date, javaScriptDec, javaScriptLink,javaScriptStar,url,javaScriptEnd];
                  NSString *htmlString = [myURLString  stringByReplacingOccurrencesOfString:@"src=\"//" withString:@"src=\"https://"];
                 _webView.delegate = self;
                 [_webView loadHTMLString:htmlString baseURL:nil];
                  }

最佳答案

您可能想看看 NSURLCache .由于 AFNetworking 是建立在 NSURLConnection 之上的,因此在应用程序委托(delegate)中设置缓存应该完全符合您的要求。

关于ios - 如何实现 JSON 的离线缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26062712/

相关文章:

ios - Async_Dispatch 线程

ios - 七次显示CCSprite

objective-c - 当 iOS 设备锁定或应用程序处于后台时启动下载

javascript - 如何使用 React JS 从 JSON 文件中获取嵌套对象

php - codeigniter db->delete() 总是返回 true 吗?

ios - 在 iOS 上的 Swift 中获取当前在 chromecast 上播放的媒体的 mediaSessionID

ios - 公司外部的企业 iOS 应用分发

ios - 如何使用 NSUserDefaults 保存和加载 UITextField?

ios - 用 Swift 模拟

java - Linkedhashmap里面的ArrayList of Objects,获取数据