我的应用中有一些uiwebview
,显示托管在服务器上的html文件。如果没有互联网连接,我也想要显示html文件的本地副本,但我不知道如何做到这一点。我的.m文件类似于下面。
目前,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 :(得分:0)
应用UIWebView
和-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
加载本地html文件的代理。
默认情况下,如果互联网存在-(void)webViewDidFinishLoad:(UIWebView *)webView
,则会执行,您将看到根据网址的页面。
答案 1 :(得分:0)
您可以将html文件的数据存储到app 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]]];
答案 2 :(得分:0)
使用webView的委托方法并在ContactMeViewController.m中实现它
在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];
}