WebView加载文件方式
来源:互联网 发布:c语言机器人编程 编辑:程序博客网 时间:2024/06/05 15:44
转载内容来自:http://www.jianshu.com/p/e8c63551ef84
1. 加载html资源
- 直接加载url 网络上的网页
[self.webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];
加载本地的html文件
利用fileURLWithPath
根据fileurl加载
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@test.html",NSTemporaryDirectory()]];[self.webView loadFileURL:url allowingReadAccessToURL:url];
利用loadHTMLString
加载htmlString
NSString *html = [NSString stringWithFormat:@"<html><head lang='en'><meta charset='UTF-8'></head><body> <div style='margin-top: 100px'><h1>图片显示测试</h1><p style='width:580px;height: 539px;background: blue' onclick='picCallback()'>[站外图片上传中……(2)]</p><input type='button' value='CallCamera' onclick='OCModel.showAlertMsg(1,2)'></div><script type='text/javascript'>var picCallback = function(photos) {alert('photos');}var shareCallback = function(){alert('success');}</script></body></html>"];// 用到的图片资源所在的路径NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@",NSTemporaryDirectory()]];[self.webView loadHTMLString:html baseURL:url];
注意:baseURL参数要传入资源文件的目录,比如html中使用到了'test.jpg',在tmp目录下。
- (nullable WKNavigation *)loadData:(NSData *)data MIMEType:(NSString *)MIMEType characterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL *)baseURL
加载data 此方法可以加载大部分别的格式NSString *path = [NSString stringWithFormat:@"%@test.html",NSTemporaryDirectory()];NSData *Data = [NSData dataWithContentsOfFile:path];[self.webView loadData:Data MIMEType:@"text/html" characterEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
2. 加载其他类型资源(doc,txt,pdf)
[self.webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];
加载本地的html文件
利用
fileURLWithPath
根据fileurl加载NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@test.html",NSTemporaryDirectory()]];[self.webView loadFileURL:url allowingReadAccessToURL:url];
利用
loadHTMLString
加载htmlStringNSString *html = [NSString stringWithFormat:@"<html><head lang='en'><meta charset='UTF-8'></head><body> <div style='margin-top: 100px'><h1>图片显示测试</h1><p style='width:580px;height: 539px;background: blue' onclick='picCallback()'>[站外图片上传中……(2)]</p><input type='button' value='CallCamera' onclick='OCModel.showAlertMsg(1,2)'></div><script type='text/javascript'>var picCallback = function(photos) {alert('photos');}var shareCallback = function(){alert('success');}</script></body></html>"];// 用到的图片资源所在的路径NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@",NSTemporaryDirectory()]];[self.webView loadHTMLString:html baseURL:url];
注意:baseURL参数要传入资源文件的目录,比如html中使用到了'test.jpg',在tmp目录下。
- (nullable WKNavigation *)loadData:(NSData *)data MIMEType:(NSString *)MIMEType characterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL *)baseURL
加载data 此方法可以加载大部分别的格式NSString *path = [NSString stringWithFormat:@"%@test.html",NSTemporaryDirectory()];NSData *Data = [NSData dataWithContentsOfFile:path];[self.webView loadData:Data MIMEType:@"text/html" characterEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
上述用到的fileURLWithPath
和- loadData:(NSData *)data MIMEType:(NSString *)MIMEType characterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL *)baseURL
也可以用来加载其他资源文件
fileURLWithPath
txt文件
doc文件NSString *path = [[NSBundle mainBundle] pathForResource:@"readme" ofType:@"txt"];[self.webView loadFileURL:[NSURL fileURLWithPath:path] allowingReadAccessToURL:[NSURL fileURLWithPath:path]];
这里要注意一点,如果doc只在main bundle中使用的,拖入工程文件的时候可能会读取不到对应的path。NSString *path = [[NSBundle mainBundle] pathForResource:@"cwm.doc" ofType:nil];[self.webView loadFileURL:[NSURL fileURLWithPath:path] allowingReadAccessToURL:[NSURL fileURLWithPath:path]];
原因我们拉入其他资源文件(比如:图片、代码文件等)Xcode都会自动添加到target 的 "Build Phases" 下 "Copy Bundle Resources目录下,但是我们拉过来的文件这里是找不到的,可能是Xcode无法识别这类文件(当我们拉去过来时),所以我们要做的就是把刚才添加的doc文件加入该目录下
如下图
pdf文件
NSString *path = [[NSBundle mainBundle] pathForResource:@"iOS" ofType:@"pdf"]; [self.webView loadFileURL:[NSURL fileURLWithPath:path] allowingReadAccessToURL:[NSURL fileURLWithPath:path]];
- loadData:
txt文件
doc文件NSString *path = [NSString stringWithFormat:@"%@readme.txt",NSTemporaryDirectory()]; NSData *Data = [NSData dataWithContentsOfFile:path]; [self.webView loadData:Data MIMEType:@"text/plain" characterEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
pdf文件NSString *path = [NSString stringWithFormat:@"%@cwm.doc",NSTemporaryDirectory()]; NSData *Data = [NSData dataWithContentsOfFile:path]; [self.webView loadData:Data MIMEType:@"application/vnd.openxmlformats-officedocument.wordprocessingml.document" characterEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
NSString *path = [NSString stringWithFormat:@"%@iOS.pdf",NSTemporaryDirectory()]; NSData *Data = [NSData dataWithContentsOfFile:path]; [self.webView loadData:Data MIMEType:@"application/pdf" characterEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
0 0
- WebView加载文件方式
- webView 网页加载方式
- WebView加载html文件
- webview加载网页有三种方式
- 3中webView加载方式
- Email正文webview加载方式
- WebView加载方的方式
- 修改WebView默认加载方式
- iOS:WebView 加载PDF文件
- 用webView加载office文件
- WebView加载本地Html文件
- webview加载本地html文件
- WebView加载assets文件乱码
- WebView加载本地HTML文件
- ios用webview文件加载
- 57.加载json文件 webview加载网页
- 用webView加载的方式拨打电话
- 加载webView的两种方式
- OLTP和OLAP区别
- 训练自己的Adaboost级联分类器并识别物体(三)
- Gulp自动构建Web前端程序
- 【Android】 toolbar左边留有空白、toolbar两端空白、toolbar两端不能填满布局问题
- android 内部sdcard出错处理errors=remount-ro改为errors=continue
- WebView加载文件方式
- osg::Group派生类速查
- Quartz2.x学习笔记(一)
- spring事务传播属性和隔离级别
- 近5年133个Java面试问题列表
- linux下 java 创建中文文件名乱码问题
- NSQ 安装指南
- MongoDB的初次相遇(三)
- 蓝桥杯 01字串 C语言