0
votes

I've come across many ways to load and reload the content of a UIWebView from a local html file but it seems like the issue I have keeps coming back.

On my project I have a webView that dynamically loads HTML content pulled from one of the 50 different HTML files I have in my XCode project. Depending on which button the user presses on the viewController before, a different HTML file is used to reload the content of my webView. Everything works fine most of the time but every now and then, the webView will simply display "(null)", after the webViewDidFinishLoad delegate method is called.

I don't understand what is going on, and when I go back and forth on this viewController and the webView is reloaded the HTML content ends up showing up properly eventually for the same HTML file.

Here is the code to load the HTML in the webView (called from the viewWillAppear method) :

  - (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]]];
}

and this is the webViewDidFinishLoad delegate method :

    - (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;
        }
}

Edit : Forgot to copy-paste the line that actually loads the HTML string in the webView

1
Where is the code to actually load the web view? Also, I don't think you should be releasing it since you're assigning it to a property which does all that stuff for you, even without ARC...jjv360
Just edited the post to add the missing line that actually load the webView. I removed the release statement and added an autorelease after the allocation of the webViewBroco
What is output when you do NSLog(@"%@", [NSData dataWithContentsOfFile:fileName]); just before you actually load the HTML? When it says (null) I mean...jjv360
I print the HTML string using : NSLog(@"%@",[NSString stringWithUTF8String:[[NSData dataWithContentsOfFile:fileName] bytes]]); the HTML content is here, I have "<body> bla bla bla </body>" but I also have an extra character "t" right after the closing </body> tag, that seems to be causing the (null)Broco
That's quite odd, it seems like sometimes it's loading extra characters from other HTML files and append it right after the </body> tagBroco

1 Answers

0
votes

The problem seems to be using the bytes function, which doesn't work too well if the content encoding isn't exact...

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

should be

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