UIWebView的三种加载方式

来源:互联网 发布:mac装双系统的利弊 编辑:程序博客网 时间:2024/06/05 04:52

UIWebView是iOS sdk中一个最常用的控件。是内置的浏览器控件,我们可以用它来浏览网页、打开文档等等。这篇文章我将使用这个控件,做一个简易的浏览器

1.加载本地的二进制文件:

NSData *data = [NSData dataWithContentsOfFile:path];            [self.webView loadData:data MIMEType:@"application/vnd.openxmlformats-officedocument.wordprocessingml.document" textEncodingName:@"UTF-8" baseURL:nil]; 

2.加载本地的html文件:

SString *resourcePath = [ [NSBundle mainBundle] resourcePath];NSString *filePath = [resourcePath stringByAppendingPathComponent:@"loading.html"];NSString *htmlstring=[[NSString alloc] initWithContentsOfFile:filePath  encoding:NSUTF8StringEncoding error:nil];   [myWebView loadHTMLString:htmlstring  baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];    [htmlstring release];

3.加载网络请求:

NSURL *url =[NSURL URLWithString:urlString];    NSLog(urlString);    NSURLRequest *request =[NSURLRequest requestWithURL:url];    [webView loadRequest:request];

注意:加载本地沙盒中的html文件时,采用第二种加载方式无用(亲测),需要使用第三种方式:

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];    NSString *savePathWeb = [path stringByAppendingPathComponent:down.webName];    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:savePathWeb]];    [self.webView loadRequest:request];


0 0
原创粉丝点击