Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does loadHTMLString:baseURL:

I new to iOS programming and tried to figure out what loadHTMLString:baseURL: really does, but I can't find a satisfying explanation. The site of Apple just says:

Sets the main page content and base URL.

Can someone please explain this in a more detailed way to me?

like image 329
user1075871 Avatar asked Dec 02 '11 09:12

user1075871


3 Answers

I am pretty certain that the baseURL is used just like in regular web pages to properly load ressources that are referenced using relative links. Now the question is, how to set that base URL to a particular folder in the app directory.

like image 183
asiby Avatar answered Oct 05 '22 05:10

asiby


This is how mainly content is loaded in a webView. either from a local html file or through a url.

//this is to load local html file. Read the file & give the file contents to webview.
[webView loadHTMLString:someHTMLstring baseURL:[NSURL URLWithString:@""]]; 

//if webview loads content through a url then 
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]]
like image 37
Srikar Appalaraju Avatar answered Oct 05 '22 05:10

Srikar Appalaraju


- (void) loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL; 

is used to load local HTML file, parameter string means content of html file, if your HTML file contains some href tag with relative path, you should set the parameter baseUrl with the base address of the HTML file, or set it nil.

NSString *cachePath = [self cachePath];
NSString *indexHTMLPath = [NSString stringWithFormat:@"%@/index.html", cachePath];
if ([self fileIsExsit:indexHTMLPath]) {
    NSString *htmlCont = [NSString stringWithContentsOfFile:indexHTMLPath
                                                            encoding:NSUTF8StringEncoding
                                                               error:nil];
    NSURL *baseURL = [NSURL fileURLWithPath:cachePath];
    [self.webView loadHTMLString:htmlCont baseURL:baseURL];
}

- (NSString *)cachePath
{
    NSArray* cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    return [cachePath[0] stringByAppendingPathComponent:@"movie"];
}
like image 29
paul Avatar answered Oct 05 '22 05:10

paul