Xcode & WebView: loading local html if there is no internet connection (backup)

I am currently programming an application for iOS using UIWebView. My goal is to show the php site (from my web server) using WebView. I am pretty good with HTMl, CSS, JS and PHP, but Object C is not my strength. However, I managed to implement everything, and now my goal (when iOS does not have an Internet connection) is to display a local file instead of a file on the server after an error warning. After using Google, I was able to do it myself, but not as a reserve.

Now it displays a warning, but after clicking the ok button, the user receives a blank page. Not very convenient :( In the local html file, I could implement a kind of β€œRefresh” button. I would be very happy if you have a (better?) Solution. Thank you!

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 
+6
source share
1 answer

You can implement the following UIAlertViewDelegate method:

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

this method is called when alertView is fired, so in its body you can load your local resources as a backup.

in the interface of your ViewController you should add:

 @interface ViewController : UIViewController <UIWebViewDelegate,UIAlertViewDelegate> 

and when you assign alertView, set the delegate to itself.

I meant this:

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Keine Internetverbindung" message:@"Bitte verbinde dich mit dem Internet." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
+2
source

Source: https://habr.com/ru/post/927751/


All Articles