UIWebView详解

UIWebView的三种加载方式和其代理
API 提供了三种方法:

1.- (void)loadRequest:(NSURLRequest *)request; //加载HTML链接地址
2.- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL; //加载HTML代码

3.- (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];
<span style="font-family: Consolas, 'Bitstream Vera sans Mono', 'Courier new', Courier, monospace; background-color: rgb(255, 255, 255);"> </span>

三、 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值