ios学习网络------4 UIWebView以三种方式中的本地数据
2015-07-21 17:51
543 查看
UIWebView这是IOS内置的浏览器。能够浏览网页,打开文档 html/htm pdf docx txt等待格文档类型。
safari浏览器是通过UIWebView制作。
server将MIME的标识符等放入传送的数据中告诉浏览器使用那种插件读取相关文件。
uiwebview载入各种本地文件(通过loadData方法):
uiwebview载入各种本地文件(通过loadRequest方法):
UIWebView载入内容的三种方式:
1 载入本地数据文件
指定文件的MIMEType
编码格式使用@“UTF-8”
2载入html字符串(能够载入所有或者部分html文件)
3载入NSURLRequest文件(前两步与NSURLConnect同样)
safari浏览器是通过UIWebView制作。
server将MIME的标识符等放入传送的数据中告诉浏览器使用那种插件读取相关文件。
uiwebview载入各种本地文件(通过loadData方法):
- (void)viewDidLoad { [super viewDidLoad]; [self setupUI]; NSString *path = [[NSBundle mainBundle] pathForResource:@"关于.docx" ofType:nil]; NSURL *url = [NSURL fileURLWithPath:path]; NSLog(@"%@", [self mimeType:url]); //webview载入本地文件。能够使用载入数据的方式 //第一个诶參数是一个NSData。 本地文件相应的数据 //第二个參数是MIMEType //第三个參数是编码格式 //相对地址,一般载入本地文件不使用,能够在指定的baseURL中查找相关文件。 //以二进制数据的形式载入沙箱中的文件。 NSData *data = [NSData dataWithContentsOfFile:path]; [self.webView loadData:data MIMEType:@"application/vnd.openxmlformats-officedocument.wordprocessingml.document" textEncodingName:@"UTF-8" baseURL:nil]; } #pragma mark 载入docx文件 - (void)loadDOCX { NSString *path = [[NSBundle mainBundle] pathForResource:@"关于.docx" ofType:nil]; NSURL *url = [NSURL fileURLWithPath:path]; NSLog(@"%@", [self mimeType:url]); NSData *data = [NSData dataWithContentsOfFile:path]; [self.webView loadData:data MIMEType:@"application/vnd.openxmlformats-officedocument.wordprocessingml.document" textEncodingName:@"UTF-8" baseURL:nil];} #pragma mark 载入pdf文件 - (void)loadPDF { NSString *path = [[NSBundle mainBundle] pathForResource:@"iOS6Cookbook.pdf" ofType:nil]; NSURL *url = [NSURL fileURLWithPath:path]; NSLog(@"%@", [self mimeType:url]); NSData *data = [NSData dataWithContentsOfFile:path]; [self.webView loadData:data MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:nil]; } #pragma mark 载入本地文本文件 - (void)loadText { NSString *path = [[NSBundle mainBundle] pathForResource:@"关于.txt" ofType:nil]; NSURL *url = [NSURL fileURLWithPath:path]; NSLog(@"%@", [self mimeType:url]); NSData *data = [NSData dataWithContentsOfFile:path]; [self.webView loadData:data MIMEType:@"text/plain" textEncodingName:@"UTF-8" baseURL:nil]; } #pragma mark 载入本地html文件 - (void)loadHTML { NSString *path = [[NSBundle mainBundle] pathForResource:@"demo.html" ofType:nil]; NSURL *url = [NSURL fileURLWithPath:path]; NSLog(@"%@", [self mimeType:url]); NSData *data = [NSData dataWithContentsOfFile:path]; [self.webView loadData:data MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:nil]; } #pragma mark 获取指定URL的MIMEType类型 - (NSString *)mimeType:(NSURL *)url { //1NSURLRequest NSURLRequest *request = [NSURLRequest requestWithURL:url]; //2NSURLConnection //3 在NSURLResponse里,server告诉浏览器用什么方式打开文件。 //使用同步方法后去MIMEType NSURLResponse *response = nil; [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; return response.MIMEType; }
uiwebview载入各种本地文件(通过loadRequest方法):
- (void)viewDidLoad { [super viewDidLoad]; [self setupUI]; //载入路径的第一个方式 // NSString *path = [[NSBundle mainBundle] pathForResource:@"关于.docx" ofType:nil]; // NSURL *url = [NSURL fileURLWithPath:path]; //载入路径的第二个方式 NSURL *url = [[NSBundle mainBundle] URLForResource:@"iOS6Cookbook.pdf" withExtension:nil]; //uiwebview载入文件的第二个方式。第一个方式在以下的凝视中。 NSURLRequest *request = [NSURLRequest requestWithURL:url]; [self.webView loadRequest:request]; //NSLog(@"%@", [self mimeType:url]); //webview载入本地文件。能够使用载入数据的方式 //第一个诶參数是一个NSData。 本地文件相应的数据 //第二个參数是MIMEType //第三个參数是编码格式 //相对地址。一般载入本地文件不使用,能够在指定的baseURL中查找相关文件。 //以二进制数据的形式载入沙箱中的文件。 // NSData *data = [NSData dataWithContentsOfFile:path]; // // [self.webView loadData:data MIMEType:@"application/vnd.openxmlformats-officedocument.wordprocessingml.document" textEncodingName:@"UTF-8" baseURL:nil]; }
UIWebView载入内容的三种方式:
1 载入本地数据文件
指定文件的MIMEType
编码格式使用@“UTF-8”
2载入html字符串(能够载入所有或者部分html文件)
3载入NSURLRequest文件(前两步与NSURLConnect同样)
相关文章推荐
- curl wget 不验证证书进行https请求
- 有关使用HTTP协议传输二进制文件
- 测试网络连通
- Linux中TCP listen()的backlog参数详解
- iOS_网络请求_代理方式
- Node.js 的 http 模块
- Java基础—socket--TCP-客户端服务器模式
- iOS_POST_网络请求
- GDB 单步调试 http://www.cppblog.com/baby-fly/archive/2010/07/27/121395.html
- iOS_GET_网络请求
- 该伙伴事务管理器已经禁止了它对远程/网络事务的支持
- Linux中TCP和UDP发送和接收缓冲区
- iOS开发中使用AFNetWorking进行https请求
- 网络字体/图标字体初体验
- 隧道和网络虚拟化:NVGRE vs VXLAN
- ./configure: error: the HTTP rewrite module requires the PCRE library
- linux脚本检测本机与指定IP段网络是否畅通
- httpd 命令解释
- PC-网络教程之宽带小型组网方案
- BAT-使用BAT方法判断网络启动EXE(快捷方式)