Load html file with images to UIWebView

Sometimes you want to load a html file with images into a UIWebView on iOS SDK. This is how you can do it, this will work but will not load images that you have in html.

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"tab1" ofType:@"html"];
    
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[self.webViewInfoText loadHTMLString:htmlString baseURL:nil];

To make it so images are loaded we have to loadHtmlString with baseURL as you can see in above example baseURL was nil. So we will put the baseURL as the mainBundle - bundlePath

NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
    
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"tab1" ofType:@"html"];
    
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[self.webViewInfoText loadHTMLString:htmlString baseURL:baseURL];

Categories

Archive