您的位置:首页 > 理论基础 > 计算机网络

ios学习网络------4 UIWebView以三种方式中的本地数据

2015-07-21 17:51 543 查看
UIWebView这是IOS内置的浏览器。能够浏览网页,打开文档 html/htm pdf docx txt等待格文档类型。

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同样)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: