UIWebView的使用、oc与js的交互、url的常用属性

基本使用:

1、创建url

 NSURL *url = [NSURL URLWithString:@"http://m.baidu.com"];

2、创建请求

// 创建请求

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

3、加载请求

[self.webView loadRequest:request];


其他:

     // 导航

    // 前进

    [self.webView goBack];

    // 后退

    [self.webView goForward];

    // 重新加载

    [self.webView reload];

    // 取消加载

    [self.webView stopLoading];



#pragma mark - webView的代理方法

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    // 返回YES: 允许加载

    returnYES;  

}

// 网页刚开始加载的时候调用

- (void)webViewDidStartLoad:(UIWebView *)webView{

}

// 网页加载完成的时候调用

- (void )webViewDidFinishLoad:(UIWebView  *)webView{

}

//网页加载错误的时候调用

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

    [selfshowSuccessOrFailedMessage:@"请求链接出错"];

}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

URL的属性

例:http://write.blog.youkuaiyun.com/postedit/49762515

request.URL.absoluteString   绝对路径:http://write.blog.youkuaiyun.com/postedit/49762515

request.URL.relativeString   relativeString: http://write.blog.youkuaiyun.com/postedit/49762515

request.URL.pathComponents    Path components as array: (

    "/",

    postedit,

    49762515

)

request.URL.lastPathComponent    lastPathComponent: 49762515

request.URL.Scheme   协议头 Scheme: http  

request.URL.Host    Host: write.blog.youkuaiyun.com   主机地址

Web与OC的交互

方法1:拦截url

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    NSLog(@"%@",request.URL.absoluteString);

    NSLog(@"%@",request.URL.pathComponents);

    NSLog(@"%@", request);

    // 拦截跳转的url,匹配最后一部分的地址

    if ([request.URL.pathComponents[3]isEqualToString:@"invetelist.html"]) {

        [MobClick event:@"shop_record"];

        CKCardExchangeListController *cardExchangeVC = [[CKCardExchangeListController alloc] init];

        cardExchangeVC.urlString = request.URL.pathComponents[3];

        [self.navigationController pushViewController:cardExchangeVC animated:YES];

        returnNO;

    }elseif ([request.URL.pathComponents[3]isEqualToString:@"chuangkeBaby.html"]){

        returnYES;

    }else{

        returnYES;

    }

}


方法2:拦截js的onclick方法 (可以拦截多个方法)

- (void )webViewDidFinishLoad:{

    JSContext *context=[webViewvalueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]; //获取js环境(固定格式)

    context[@"getFight"] = ^() {//点击调用  onclickjs点击调用的函数名 其实就是把在js里面的函数调来在oc里面实现    就是oc里面的block实现对应js里面的函数体        

        NSArray *args = [JSContextcurrentArguments]; //args里面装的是onClick(...)里面的参数列表,不是按顺序排列

        for (JSValue *jsValin args) {

            NSLog(@"jsVal:%@", jsVal);

        }        

        JSValue *this = [JSContextcurrentThis];

        NSLog(@"this:%@",this);

        NSLog(@"-------End Log-------");

            CKLoginRootViewController * _Login = [CKLoginRootViewControllernew];

            UINavigationController * nav = [[UINavigationControlleralloc] initWithRootViewController:_Login];

            [self  presentViewController:nav animated:YEScompletion:^{

            };

    

    context[@"toDuihuanRecord"] = ^() {//点击调用  onclickjs点击调用的函数名 其实就是吧在js里面的函数调来在oc里面实现    就是oc里面的block实现对应js里面的函数体        

        NSArray *args = [JSContextcurrentArguments];//args里面装的是onClick(...)里面的参数列表,不是按顺序排列        

        for (JSValue *jsValin args) {

            NSLog(@"jsVal:%@", jsVal);

        }        

        JSValue *this = [JSContextcurrentThis];

        NSLog(@"this:%@",this);

        NSLog(@"-------End Log-------");       

        CKCardExchangeListController *cardExchangeVC = [[CKCardExchangeListControlleralloc] init];

        cardExchangeVC.urlString =@"invetelist.html";

        [self.navigationControllerpushViewController:cardExchangeVCanimated:YES];

    };

    

    context[@"gotomyRedpacket"] = ^() {//点击调用  onclickjs点击调用的函数名 其实就是吧在js里面的函数调来在oc里面实现    就是oc里面的block实现对应js里面的函数体

        CKMyFavorableController *_CKMyFavorableController = [[CKMyFavorableControlleralloc] init];

        [self.navigationControllerpushViewController:_CKMyFavorableControlleranimated:YES]; 

    };    

}


方法3:oc调用js

/**

    我们在这个方法里面执行js函数,去调用我们js的方法

    UIWebView主动调用JS的函数

 */

- (void)webViewDidFinishLoad:(UIWebView *)webView{

    NSString *result = [webView  stringByEvaluatingJavaScriptFromString:@"javascript:add(1,2);"];

    NSLog(@"oc====%@",result);


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值