2012-11-05 63 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];