I’m building an article reading app like TechCrunch.I’m fetching data from server that is JSON content and display JSON content in UITableView like Article image,title and author name.
When user click on an article it opens in UIWebView.
I have passéd the Title,author name and content of article through segue which i have already fetched in UITableView.
I have integrated third party library in my project that is AFNetworking.I want to implement offline caching of articles means if there is no internet user can read articles.
This my code:
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;\">%@  </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];
}
You might want to have a look at NSURLCache. Since AFNetworking is built on top of NSURLConnection setting up cache in app delegate should do exactly what you are trying to do.