3

I need to make an offline browser on my iOS application, which allows the following:

When you have an internet connection (wifi, 3g, 4g, ...) you can download all the web pages you need to read during the day, then you can browse the content also when you have no internet connection. I import the data from JSON file.

How can I do this?

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
Fatima
  • 464
  • 1
  • 8
  • 19

1 Answers1

0

If you have the data downloaded, save it to the documents directory and check if no internet connection, then load the content from the documents directory.

Get Documentpath

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];

If you have an NSDictionary:

NSString *path = [NSString stringWithFormat:@"%@",[paths objectAtIndex:0]];
NSDictionary *dict = [[NSDictionary alloc] init];
[dict writeToFile:[path stringByAppendingPathComponent:@"myOfflineData"] atomically:NO];

And to get it:

NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:@"myOfflineData"]];

Or save it with a NSString etc.

To catch up your Example:

NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http:......."]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]; jsonResults = [jsonObjects objectForKey:@"nodes"];
[jsonData writeToFile:[path stringByAppendingPathComponent:@"myOfflineData"] atomically:NO];

And if you are offline:

NSData *jsonData = [[NSData alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:@"myOfflineData"]];
Julien Klindt
  • 313
  • 2
  • 12
  • let me try it, then i will give you a feedback – Fatima Jul 23 '13 at 08:17
  • look, where should I include this code?, the code that I get the data is: `NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http:......."]]; id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]; jsonResults = [jsonObjects objectForKey:@"nodes"];` which jsonresults is a mutable array – Fatima Jul 23 '13 at 08:26
  • just save the nsdata to your documents and load it when you are offline. just replace nsdictionary with nsdata – Julien Klindt Jul 23 '13 at 08:29
  • I will test it, then I will tell you if it will work thank you – Fatima Jul 23 '13 at 12:38
  • Can you tell me in which method I will implement it, in fact, in the root view I implement the code above, in the appdelegate I implement an alert to the user to tell that No connection. The storage of data and to get it where should I implement it? – Fatima Jul 26 '13 at 07:19
  • You could just implement it in your rootView and use the Reachability class from Apple to check if there is no connection or not, this logic should not be placed in the appDelegate though. http://developer.apple.com/library/ios/#samplecode/Reachability/ – Julien Klindt Jul 30 '13 at 09:02