UIWebView从本地文件加载HTML但偶尔会失败

时间:2012-11-05 12:40:33

标签: objective-c ios uiwebview uiwebviewdelegate

我遇到过很多方法从本地html文件加载和重新加载UIWebView的内容,但似乎我不断回来的问题。

在我的项目中,我有一个webView,可以动态加载从我在XCode项目中的50个不同HTML文件之一中提取的HTML内容。 根据用户之前在viewController上按下的按钮,使用不同的HTML文件重新加载webView的内容。大多数时候一切正常,但在调用webViewDidFinishLoad委托方法之后,webView将只显示“(null)”。

我不明白发生了什么,当我在这个viewController上来回转换并重新加载webView时,HTML内容最终会在同一个HTML文件中正确显示。

以下是在webView中加载HTML的代码(从viewWillAppear方法调用):

  - (void) reloadWebViewFromLocalFile{

    // Reset the UIWebView
    [self.contentWebView removeFromSuperview];
    self.contentWebView = nil;
    self.contentWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
    [self.contentWebView setContentMode:UIViewContentModeScaleAspectFit];
    [self.contentWebView setDelegate:self];
    [self.contentScrollView addSubview:self.contentWebView];
    [self.contentWebView release];

    NSString *fileName = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d.%d",self.currentIndexPath.section+1,self.currentIndexPath.row+1] ofType:@"html"];

    NSString *CSSContent = [NSString stringWithFormat:@"<style type=\"text/css\">body{padding-left:8px;padding-right:8px;font-family:\"%@\";}p{text-align:justify;}img{max-width:300px;display:block; margin:auto;}strong{font-family:\"%@\";}h1{font-size:20;font-family:\"%@\"}h2{font-size:18;font-family:\"%@\"}h3{font-size:17;font-family:\"%@\"}</style><script type=\"text/javascript\">touchMove = function(event) {event.preventDefault();}</script>", FONT_STAG_BOOK, FONT_STAG_SEMIBOLD, FONT_STAG_SEMIBOLD, FONT_STAG_SEMIBOLD, FONT_STAG_SEMIBOLD];

    self.HTMLString = [NSString stringWithFormat:@"<html><head>%@</head>%@</html>",CSSContent,[NSString stringWithUTF8String:[[NSData dataWithContentsOfFile:fileName] bytes]]];
[self.contentWebView loadHTMLString:self.HTMLString baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
}

这是webViewDidFinishLoad委托方法:

    - (void)webViewDidFinishLoad:(UIWebView *)webView{

    NSString *stringContent = [self.contentWebView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];

        float height = [[self.contentWebView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue] + 20;
        [self.contentWebView setFrame:CGRectMake(0, 0, 320, height)];
        [self.contentScrollView setContentSize:CGSizeMake(0, height+self.nextButton.frame.size.height)];

        [self.nextButton setFrame:CGRectMake(0, height, 320, self.nextButton.frame.size.height)];

        if ([self.contentWebView respondsToSelector:@selector(scrollView)]) {
            self.contentWebView.scrollView.scrollEnabled = NO; // available starting in iOS 5
        } else {
            for (id subview in self.contentWebView .subviews)
                if ([[subview class] isSubclassOfClass: [UIScrollView class]])
                    ((UIScrollView *)subview).scrollEnabled = NO;
        }
}

编辑:忘记复制粘贴在webView中实际加载HTML字符串的行

1 个答案:

答案 0 :(得分:0)

问题似乎是使用bytes函数,如果内容编码不准确,则该函数效果不佳......

[NSString stringWithUTF8String:[[NSData dataWithContentsOfFile:fileName] bytes]];

应该是

[[NSString alloc] initWithData:[NSData dataWithContentsOfFile:fileName] encoding:NSUTF8StringEncoding];