0

我遇到了许多从本地 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 字符串的行

4

1 回答 1

0

问题似乎出在使用该bytes功能,如果内容编码不准确,它就不能很好地工作......

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

应该

[[NSString alloc] initWithData:[NSData dataWithContentsOfFile:fileName] encoding:NSUTF8StringEncoding];
于 2012-11-05T14:15:44.003 回答