objective-c – Xcode和WebView:如果没有互联网连接,则加载本地
发布时间:2020-12-14 19:39:34 所属栏目:百科 来源:网络整理
导读:目前我正在使用UIWebView为iOS编写应用程序.我的目标是使用WebView显示一个php站点(来自我的web服务器).我对HTMl,CSS,JS和 PHP非常好,但是对象C不是我的优势.但是我设法实现了一切,我的目标是(当iOS没有互联网连接时)在错误警报后显示本地文件而不是服务器上
目前我正在使用UIWebView为iOS编写应用程序.我的目标是使用WebView显示一个php站点(来自我的web服务器).我对HTMl,CSS,JS和
PHP非常好,但是对象C不是我的优势.但是我设法实现了一切,我的目标是(当iOS没有互联网连接时)在错误警报后显示本地文件而不是服务器上的文件.使用谷歌后,我设法独立完成,但不是作为后备.
现在它显示警报,但点击确定后,用户获得一个空白页面.不是非常用户友好:(在本地html文件中我可以实现一种“刷新按钮”.如果你有一个(更好的?)解决方案,我会很高兴.谢谢! 我的系统:OS X 10.8.2上的Xcode 4.5.1 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 解决方法
您可以实现以下UIAlertViewDelegate方法:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 当alertView被解除时调用此方法,因此在他的身体中,您可以将本地资源作为后备加载. 在ViewController的界面中,你应该添加: @interface ViewController : UIViewController <UIWebViewDelegate,UIAlertViewDelegate> 当你分配你的alertView时,将委托设置为self. 我的意思是: UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Keine Internetverbindung" message:@"Bitte verbinde dich mit dem Internet." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |