xcode 什么 loadHTMLString:baseURL:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8354394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does loadHTMLString:baseURL:
提问by user1075871
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:
我是 iOS 编程的新手,并试图弄清楚loadHTMLString:baseURL:
真正做了什么,但我找不到令人满意的解释。苹果的网站只是说:
Sets the main page content and base URL.
设置主页内容和基本 URL。
Can someone please explain this in a more detailed way to me?
有人可以更详细地向我解释这一点吗?
回答by asiby
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.
我很确定 baseURL 就像在常规网页中一样使用,以正确加载使用相对链接引用的资源。现在的问题是,如何将该基本 URL 设置为应用程序目录中的特定文件夹。
回答by Srikar Appalaraju
This is how mainly content is loaded in a webView. either from a local html file or through a 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"]]]
回答by paul
- (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
.
用于加载本地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"];
}