【问题标题】:how to load a local html file if there is no internet connection如果没有互联网连接,如何加载本地 html 文件
【发布时间】:2015-03-25 17:56:19
【问题描述】:

我的应用中有许多 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

【问题讨论】:

标签: ios xcode uiwebview


【解决方案1】:

UIWebView 应用代理并在-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error 中加载本地html 文件。

默认情况下,如果有互联网 -(void)webViewDidFinishLoad:(UIWebView *)webView,将被执行,您将看到按照 URL 的页面。

【讨论】:

    【解决方案2】:

    您可以将html文件的数据作为.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]]];    
    

    【讨论】:

    • 嗨 poojathorat 这是否意味着应用程序可以有效地下载远程托管页面并写入本地文件?
    • 是的,下载html页面的数据并写入文档目录,这样下次你只能从文档目录加载。
    【解决方案3】:

    使用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];
    }
    

    【讨论】:

    • 您好 Tanuj,我已更新 .m 文件中的代码(请参阅主要问题编辑)。通过互联网连接,远程页面加载正常,但没有互联网连接,我得到一个空白页面。我错过了什么吗?
    • 我对 loadHtmlFile 方法做了一些改动。请再次检查。
    • 感谢您做出更改,我已相应地进行了相同的更改,但遇到相同的问题,如果没有互联网连接,则会出现空白页面
    • 您的文档文件中有“contact.html”文件吗?
    • 嘿,我的项目中有文件,我不确定我是否知道您所说的文档文件是什么意思 - 我需要设置一个包含其中的 html 文件的文件吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多