Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode & WebView: Load local html if no internet connection (fallback)

Currently I'm programming an app for iOS using UIWebView. My goal is to display a php site (from my webserver) using a WebView. I'm pretty good with HTMl, CSS, JS and PHP but Object C is not my strength. However I managed to implement everything and my goal is now (when iOS hasn't an internet connection) to display a local file instead of the file on the server after the error alert. After using Google I managed to do it independently, but not as a fallback.

Right now it displays the alert, but after a tap on ok the user gets a blank page. Not very user friendly :( In the local html file I could implement a sort of "refresh button". I would be very happy, if you have a (better?) solution. Thanks!

My System: Xcode 4.5.1 on OS X 10.8.2

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIWebViewDelegate>
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *loadingSign;
- (void)loadSite;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize webView;
@synthesize loadingSign;

-(void) webViewDidStartLoad:(UIWebView *)webView {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [self.loadingSign startAnimating];
    self.loadingSign.hidden = NO;
}

-(void) webViewDidFinishLoad:(UIWebView *)webView {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [self.loadingSign stopAnimating];
    self.loadingSign.hidden = YES;
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [self.loadingSign stopAnimating];
    self.loadingSign.hidden = YES;
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Keine Internetverbindung" message:@"Bitte verbinde dich mit dem Internet." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

- (void)loadSite
{
    NSString *fullURL = @"http://website.com";
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];
    webView.scrollView.bounces = NO;
    webView.delegate = self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self loadSite];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
like image 858
ohh2ahh Avatar asked Oct 15 '12 13:10

ohh2ahh


1 Answers

You can implement the following UIAlertViewDelegate method :

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

this method is called when the alertView is dismissed so in his body you can load your local resources as a fallback.

in the interface of your ViewController you should add :

@interface ViewController : UIViewController <UIWebViewDelegate,UIAlertViewDelegate>

and when you alloc your alertView set the delegate to self.

I meant this :

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Keine Internetverbindung" message:@"Bitte verbinde dich mit dem Internet." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
like image 139
oiledCode Avatar answered Nov 14 '22 00:11

oiledCode