Search code examples
htmlioscachinguiwebviewsegue

Offline UIWebView when internet unavailable


I have a UIWebView. When there is internet access I want it to load the actual webpage and when there is no internet I want it to load a saved html complete website file. I implemented a test webView with the local file, and it works perfectly. The code is this:

[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"My Website" ofType:@"html"]isDirectory:NO]]];

Of course I synthesized the webview and made it in the .h as well. I also made a normal webview accessing the website online, which is exactly the same except that instead of the above code in the viewDidLoad it says:

NSString *fullURL = @"http://www.mywebsite.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:requestObj];

However, when I tried making the webview load the html when no internet was available and the website when internet was available, it worked, but the offline site stopped supporting anything but the main page and lost all its photos (they work fine in the separate view, as described above, just not in the demo offline-online view). I used the following code:

NSURL *scriptUrl = [NSURL URLWithString:@"http://www.mywebsite.com"];
NSData *data = [NSData dataWithContentsOfURL:scriptUrl];
if (data) {
    NSLog(@"Device is connected to the internet");
    NSString *fullURL = @"http://www.mywebsite.com";
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [myWebView loadRequest:requestObj];

} else {
    NSLog(@"Device is not connected to the internet");
    [myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"My Website" ofType:@"html"]isDirectory:NO]]];
}

In desperation, I tried adding a navigation bar to each of my original views, so that in the offline view you could click the button saying "online" and segue to the online view, and vice versa. This failed miserably: the offline view (which was the initial one) worked fine but when I pressed the "online" button I got the following error:

*** First throw call stack: (0x321cb3e7 0x3a065963 0x321cb0d5 0x32a397d9 0x32a35543 0x321518a5 0x34176e7d 0x341766ff 0x3406f079 0x33ff9451 0x34085357 0x340c6cd9 0x340c5fab 0x341e7da3 0x340c5087 0x340c5111 0x340c5087 0x340c503b 0x340c5015 0x340c48cb 0x340c4db9 0x33fed5f9 0x33fda8e1 0x33fda1ef 0x35cce5f7 0x35cce227 0x321a03e7 0x321a038b 0x3219f20f 0x3211223d 0x321120c9 0x35ccd33b 0x3402e2b9 0x8194d 0x3a492b20) libc++abi.dylib: terminate called throwing an exception

I tried making the online view be the initial one and the offline one be the secondary one that you click a bar button to get to, and the offline one stopped working. It appears as if the initial view will load, whichever it is, but not the secondary one. Maybe I am incorrectly using bar button items, though I am experienced with storyboarding and have many bar button segues located in the rest of my project. And yes, I did change the names of my web views to be different (myWebView and myOfflineWebView) when implementing the last mentioned bar button solution, after it initially failed, because I thought maybe having two different uiWebViews with the same name, albeit in different viewControllers, might be the problem, but that didn't help.

Any feedback is appreciated. Possibly the code for checking internet availability did not work because it was made for OSX. I found it here. I would prefer not to bog down my project with big libraries (like this one, which is often mentioned) as it had many libraries all ready and is a bit slow. I cannot use the usual Apple Reachability sample project because I am using ARC. I could refactor it but I do not have much time and would prefer a simpler solution, even if it is not 100% perfect or elegant. Thank you!


Solution

  • So, regarding the internet availability, you will want to use this reachability library. Also, your images and links probably aren't loading because your saved html has absolute urls: http://foo.com/image.jpg which can't be loaded because you have no connection.