What does loadHTMLString:baseURL:



我是iOS编程新手,试图弄清楚loadHTMLString:baseURL:的真正功能,但我找不到令人满意的解释。苹果公司的网站只是说:

设置主页内容和基础URL。

谁能更详细地给我解释一下吗?

我很确定baseURL就像在普通网页中使用一样,可以正确加载使用相对链接引用的资源。现在的问题是,如何将基础URL设置为应用目录中的特定文件夹。

这是webView加载内容的主要方式。从本地HTML文件或通过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"]]]
- (void) loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL; 

用于加载本地HTML文件,参数字符串表示html文件的内容,如果您的HTML文件中包含一些带有相对路径的href标签,则应将参数baseUrl设置为HTML文件的基址,或设置为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"];
}

最新更新