2015-03-25 57 views
0

我有一個uiwebview在我的應用程序,顯示服務器上承載的HTML文件的數量。如果沒有互聯網連接,我還想要顯示將顯示的html文件的本地副本,但我不知道如何執行此操作。我有的.m文件與下面類似。如何加載本地HTML文件,如果沒有互聯網連接

目前uiwebview將顯示遠程託管的網頁(以下「contact.html」的例子。有沒有人能夠解釋,如果沒有可用的互聯網我如何加載該文件的本地副本?

#import "ContactMeViewController.h" 
@interface ContactMeViewController() 
@end 
@implementation ContactMeViewController 
- (void)viewDidLoad 
{ 
NSString *urlAddress = @"http://www.domain.co.nz/apppages/contact.html"; 
    //Create a URL object. 
NSURL *url = [NSURL URLWithString:urlAddress]; 
    //URL Request Object 
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    //Load the request in the UIWebView. 
[webview loadRequest:requestObj]; 
webview.scalesPageToFit = YES; 
} 
- (void)viewDidUnload 
{ 
[super viewDidUnload]; 
// Release any retained subviews of the main view. 
// e.g. self.myOutlet = nil; 
} 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
@end 

我已經更新了.m文件如下,卻得到了一個空白的屏幕,沒有互聯網連接,而不是本地文件加載:

#import "ContactMeViewController.h" 
@interface ContactMeViewController (UIWebViewDelegate) 
@end 
@implementation ContactMeViewController 
- (void)viewDidLoad 
{ 
NSString *urlAddress = @"http://www.kuranimarsters.co.nz/apppages/contact.html"; 

//Create a URL object. 
NSURL *url = [NSURL URLWithString:urlAddress]; 

//URL Request Object 
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 

//Load the request in the UIWebView. 
[webview loadRequest:requestObj]; 
webview.scalesPageToFit = YES; 
} 
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { 
if (-1009 == kCFURLErrorNotConnectedToInternet) { 
    // if we can identify the error i.e, no internet connection 
    [self loadHtmlFile]; 
} 
} 
-(void)loadHtmlFile 
{ 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"contact.html"]; 
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL]; 

NSString* htmlString = [NSString stringWithContentsOfFile:content encoding:NSUTF8StringEncoding error:nil]; 
[webview loadHTMLString:htmlString baseURL:nil]; 
} 
- (void)viewDidUnload 
{ 
[super viewDidUnload]; 
// Release any retained subviews of the main view. 
// e.g. self.myOutlet = nil; 
} 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
@end 
+0

使用委託,當錯誤出現,加載本地HTML網頁... – 2015-03-25 06:39:27

+0

您可以嘗試https://github.com/rnapier/RNCachingURLProtocol – PowHu 2015-03-25 06:42:14

回答

0

應用代表了UIWebView-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error負荷爲本地H tml文件。

默認情況下,如果互聯網有-(void)webViewDidFinishLoad:(UIWebView *)webView,將會執行,您將按照URL查看頁面。

0

可以存儲HTML文件的數據ContactMeViewController.m記錄應用as.html文件的目錄對下一次該文件&負載的WebView如

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0],@"index.html"]; 

// Download and write to file 
NSURL *url = [NSURL URLWithString:@"http://www.domain.co.nz/apppages/contact.html"]; 
NSData *urlData = [NSData dataWithContentsOfURL:url]; 
[urlData writeToFile:filePath atomically:YES]; 

// Load file in UIWebView 
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];  
+0

喜poojathorat這是否意味着有效的應用程序會下載遠程託管頁面並寫入本地文件? – KGNZ 2015-03-25 06:58:33

+0

是下載html頁面的數據並寫入文檔目錄,以便下次只能從文檔目錄加載。 – poojathorat 2015-03-25 07:00:29

0

webView的使用委託方法並執行它

在didFailLoadWithError方法中,您可以使用[錯誤代碼]標識錯誤代碼。您可以使用kCFURLErrorNotConnectedToInternet = -1009。

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { 
    if ([error code] == kCFURLErrorNotConnectedToInternet) { 
     // if we can identify the error i.e, no internet connection 
     [self loadHtmlFile]; 
    } 
} 

-(void)loadHtmlFile 
{ 
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"contact" ofType:@"html"]; 
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil]; 
[webview loadHTMLString:htmlString baseURL:nil]; 
} 
+0

嗨Tanuj,我更新了.m文件中的代碼(請參閱主要問題編輯)。 隨着互聯網連接,遠程頁面加載正常,但沒有互聯網連接,我得到一個空白頁。我錯過了什麼嗎? – KGNZ 2015-03-25 09:39:01

+0

我已經做了一些loadHtmlFile方法的變化。請再檢查一次。 – Tanuj 2015-03-25 09:57:36

+0

感謝您的更改,我做了相應的更改,但也有相同的問題,如果沒有互聯網連接,有空白頁 – KGNZ 2015-03-25 10:30:14

相關問題