ios 如何将本地 HTML 文件加载到 UIWebView 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4645414/
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
How can I load a local HTML file into the UIWebView?
提问by RAMAN RANA
How can we load our own html file into the UIWebView?
我们如何将我们自己的 html 文件加载到 UIWebView 中?
回答by Cody Gray
The following code will load an HTML file named index.html
in your project folder:
以下代码将加载index.html
在您的项目文件夹中命名的 HTML 文件:
[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];
回答by deanWombourne
Cody Gray is right but there's also this way :
科迪格雷是对的,但也有这种方式:
// Load the html as a string from the file system
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
// Tell the web view to load it
[WebView loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]];
This is useful if you need to edit the html before you load it.
如果您需要在加载之前编辑 html,这将非常有用。
回答by Oded Regev
Swift
迅速
guard let path = NSBundle.mainBundle().pathForResource("index", ofType: "html") else {
return
}
let url = NSURL(fileURLWithPath: path)
self.webview.loadRequest(NSURLRequest(URL:url))
回答by Teja Swaroop
By using following code you can load an html in an WebView.here web is web view obj and inde
通过使用以下代码,您可以在 WebView 中加载 html。这里的 web 是 web 视图 obj 和 inde
Web.delegate = self;
[Web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];
回答by siege_Perilous
Apple's documentation suggests using loadHTMLString:baseURL:
:
Apple 的文档建议使用loadHTMLString:baseURL:
:
Use the loadHTMLString:baseURL: method to begin loading local HTML files or the loadRequest: method to begin loading web content. Use the stopLoading method to stop loading, and the loading property to find out if a web view is in the process of loading.
使用 loadHTMLString:baseURL: 方法开始加载本地 HTML 文件或 loadRequest: 方法开始加载 Web 内容。使用 stopLoading 方法停止加载,使用 loading 属性查看 web 视图是否在加载过程中。
The loadHTMLString:baseURL:
documentation offers further reasoning:
该loadHTMLString:baseURL:
文档提供了进一步的推理:
To help you avoid being vulnerable to security attacks, be sure to use this method to load local HTML files; don't use loadRequest:.
为帮助您避免受到安全攻击,请务必使用此方法加载本地 HTML 文件;不要使用 loadRequest:。
This one might help: https://stackoverflow.com/a/29741277
这个可能有帮助:https: //stackoverflow.com/a/29741277
回答by Nupur Gupta
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"privacy-Policy" ofType:@"html" inDirectory:nil];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];
回答by Sruit A.Suk
Swift Version 2.1
斯威夫特 2.1 版
// load html to String with Encoding
let path = NSBundle.mainBundle().pathForResource("policy", ofType: "html")
do {
let fileHtml = try NSString(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
webView.loadHTMLString(fileHtml as String, baseURL: nil)
}
catch {
}