【问题标题】:How Can i implement Offline caching of JSON?如何实现 JSON 的离线缓存?
【发布时间】:2014-11-21 15:37:19
【问题描述】:

我正在构建一个像 TechCrunch 这样的文章阅读应用程序。我正在从服务器获取 JSON 内容的数据,并在 UITableView 中显示 JSON 内容,如文章图像、标题和作者姓名。

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

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

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

这是我的代码:

TableView.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]];
                         });
                       } 
                      }

Webview.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];
                  }

【问题讨论】:

    标签: ios objective-c json afnetworking offline-caching


    【解决方案1】:

    有很多方法可以将数据持久化到磁盘:例如,您可以使用 Core Data 或原始 SQLite。对于这么简单的事情,最简单的方法是将 JSON 字符串写入文件并在您想要显示文章时将其读回。看看苹果的File System Programming Guide

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2015-12-15
        • 2013-06-14
        • 2014-02-22
        • 1970-01-01
        • 2011-07-30
        • 2011-11-27
        • 1970-01-01
        • 2014-11-01
        • 2018-03-28
        相关资源
        最近更新 更多