2012-10-15 69 views
6

目前我正在使用UIWebView爲iOS設計應用程序。我的目標是使用WebView顯示一個php站點(從我的web服務器)。我對HTMl,CSS,JS和PHP相當不錯,但是對象C並不是我的優勢。然而,我設法實現了一切,我的目標是現在(當iOS沒有互聯網連接時)在錯誤警報之後顯示本地文件而不是服務器上的文件。使用谷歌後,我設法獨立完成,但不是作爲後備。Xcode&WebView:如果沒有互聯網連接(回退)加載本地html

現在它顯示警報,但在點擊確定後,用戶會得到一個空白頁。不是非常用戶友好:(在本地html文件中,我可以實現一種「刷新按鈕」,如果您有更好的解決方案,我會很高興。 1在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 
+0

加載本地HTML時發生錯誤(您需要檢查爲什麼誤差)從 - (空)webView的:(UIWebView的*)webView的didFailLoadWithError:(NSError *)錯誤 – Vjy

回答

2

可以實現以下UIAlertViewDelegate方法:

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

當alertView被解散時,調用此方法,因此在他的正文中可以加載本地資源作爲後備。

在你的ViewController的接口

你應該增加:

@interface ViewController : UIViewController <UIWebViewDelegate,UIAlertViewDelegate> 

,當你的Alloc您alertView設置委託自我。

我的意思是這樣的:

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

感謝您的快速解決方案。它完美的作品。但我不明白你的最後一句話...... – ohh2ahh

相關問題