UIWebView与JS交互

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;

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

为什么需要设置baseURL?

(1)也就是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 = @"

诚邀享有丰富教学。

整合全球优质教学资 源。

通过科学的培训。

拥有优质食品管理人员。< /li>

提高食品行业对食品安全的认知与重视度。

< /p>"

     [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代理方法

    /**

     *  webView开始发送请求的时候就会调用

     */

- (void)webViewDidStartLoad:(UIWebView *)webView

{

// 显示提醒框

[MBProgressHUD showMessage:@"哥正在帮你加载中..."];

}

/**

*  webView请求完毕的时候就会调用

*/

- (void)webViewDidFinishLoad:(UIWebView *)webView

{

// 隐藏提醒框

[MBProgressHUD hideHUD];

}

/**

*  webView请求失败的时候就会调用

*/

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

{

// 隐藏提醒框

[MBProgressHUD hideHUD];

}

/**

*  当webView发送一个请求之前都会先调用这个方法, 询问代理可不可以加载这个页面(请求)

*

*  @return YES : 可以加载页面,  NO : 不可以加载页面

*/

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType(最主要的代理方法, 用于截取webView每次跳转, 可以根据两个参数判,断是否跳转到正常的web页面还是跳转到自己写的原生页面)

{

@param request : The content location(请求)

@param navigationType : The type of user acti, on that started the load request(跳转类型详细看下面)

UIWebViewNavigationTypeLinkClicked, 用户触击了一个链接。

UIWebViewNavigationTypeFormSubmitted, 用户提交了一个表单。

UIWebViewNavigationTypeBackForward, 用户触击前进或返回按钮。

UIWebViewNavigationTypeReload, 用户触击重新加载的按钮。

UIWebViewNavigationTypeFormResubmitted, 用户重复提交表单

UIWebViewNavigationTypeOther, 发生其它行为。

if (写判断) {

// 不加载这个请求

return NO;

}

return YES;

}

iOS7之后JavaScript与Objective-C之间的通信 - CocoaChina_让移动开发更简单 

你可能感兴趣的:(UIWebView与JS交互)