UIWebView详解

来源:互联网 发布:知君用心综琼瑶阅读 编辑:程序博客网 时间:2024/05/22 10:34

UIWebView的三种加载方式和其代理

API 提供了三种方法:

- (void)loadRequest:(NSURLRequest *)request; //加载HTML链接地址- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL; //加载HTML代码- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;

(1)其中baseURL 是指基准的url 是一个绝对的地址,程序要用到的其他资源就可以根据这个基准地址进行查找而不用再次定位到绝对地址;

为什么需要设置baseURL?

也就是data中有一些链接是图片,css都是外部文件,然后这些文件需要到一个目录上去找。baseURL就是这个目录。

(2)使用loadData方法对文件进行加载,并且指定类型(mimetype)和编码类型(textEncodingName)

一、UIWebView加载.html网页文件(loadRequest)

以下代码都放在viewDidLoad方法体里:

1、加载网络html文件:

NSURL *url = [NSURL URLWithString:@"http://localhost:8080/jmDemo/index.html"]; //@"http//:www.baidu.com"NSURLRequest *request = [NSURLRequest requestWithURL:url];  [_webView loadRequest:request]; 

2、加载本地html文件,在iphone项目里面

NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];  NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]];  [_webView loadRequest:request]; 


3、加载本地html文件,不在iphone项目里面,在Documents文件夹里

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  NSString *documentsDirectory = [paths objectAtIndex:0];  NSString *path = [documentsDirectory stringByAppendingPathComponent:@"index.html"];  NSURL *url = [NSURL fileURLWithPath:path];  NSURLRequest *request = [NSURLRequest requestWithURL:url];  [_webView loadRequest:request];

附加 加载pdf格式

NSURL *url =[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"f5_newspaper" ofType:@"pdf"]];NSURLRequest *request = [NSURLRequest requestWithURL:url];[_webView loadRequest:request];

二、loadHTMLString:baseURL:(1将本地html文件内容嵌入 2.加载HTML代码 )

(1)将本地html文件内容嵌入

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

(2)直接加载加载HTML代码

NSString *htmlstring = @"

 

  • 诚邀享有丰富教学。
  • 整合全球优质教学资源。
  • 通过科学的培训。
  • 拥有优质食品管理人员。
  • 提高食品行业对食品安全的认知与重视度。

"

[self.webView loadHTMLString:htmlstring baseURL:nil];

三、 loadData:MIMEType:textEncodingName:baseURL:

(1)html内容

NSString *string = [NSString stringWithFormat:@"%@",self.model.contentClob];NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];[webContentView loadData:data MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:nil];

(2)加载.html文件

   //首先把数据写到NSData中

NSString *pathurl = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];  NSData *data = [NSData dataWithContentsOfFile:pathurl];

//然后设置baseURL

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDir = [paths objectAtIndex:0] ;   //根据自己的具体情况设置,我的html文件在document目 录,链接也是在这个目录上开始 NSURL *baseURL = [NSURL fileURLWithPath:documentsDir]; [self.webView loadData:data MIMEType:@"text/html" textEncodingName:@"GBK" baseURL:baseURL];

四 、其他属性及常用代理

 webContentView.scalesPageToFit = YES;//让web content布局适应webView webContentView.delegate = self; #pragma mark - webView代理方法 - (void)webViewDidStartLoad:(UIWebView *)webView{    // 显示提醒框    [MBProgressHUD showMessage:@"哥正在帮你加载中..."];}  - (void)webViewDidFinishLoad:(UIWebView *)webView{    // 隐藏提醒框    [MBProgressHUD hideHUD];} - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{    // 隐藏提醒框    [MBProgressHUD hideHUD];}  - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{    if (写判断) {        // 不加载这个请求        return NO;    }    return YES;}














0 0
原创粉丝点击