2012-11-05 67 views
0

我遇到过很多方法来加载和重新加载从本地HTML文件的UIWebView的内容,但它似乎像我一直在回来的问题。UIWebView从本地文件加载HTML,但偶尔失败

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

我不明白发生了什么,当我在这个viewController上来回滚动并重新加载webView时,HTML内容最终会最终显示为相同的HTML文件。

这里是加载在web视图的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字符串的行

+0

哪里是实际读取的代码网络视图?此外,我不认为你应该释放它,因为你将它分配给一个属性,即使没有ARC,为你做所有的东西...... – jjv360

+0

刚刚编辑帖子添加实际加载webView的缺失行。 我删除了发布声明,并在分配webView后添加了autorelease – Broco

+0

当你执行'NSLog(@“%@”,[NSData dataWithContentsOfFile:fileName])时输出什么;'在实际加载HTML之前?当它说(空)我的意思是... – jjv360

回答

0

问题米似乎是使用bytes功能,不工作也很好,如果内容编码不准确......

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

应该

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