js调用oc代码 和 oc调用js代码

1.js调用oc代码  oc中的网页html
<!DOCTYPE html>
<html>
<head lang="en">
    <metacharset="UTF-8">
    <title></title>
    <style>
        body{
            margin:40px;
        }
    </style>
</head>
<body>
   <buttonοnclick="openCamera();">访问相册</button>
   
   <scripttype="text/javascript">
       function openCamera(){
           window.location.href ='dswq:///openCamera';
       }
   </script>
</body>
</html>




oc的代码

- (void)viewDidLoad {
    [superviewDidLoad];
   
    NSURL *url = [[NSBundlemainBundle] URLForResource:@"test"withExtension:@"html"];
    NSURLRequest *request = [NSURLRequestrequestWithURL:url];
    [self.webViewloadRequest:request];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    NSLog(@"%@", request.URL.absoluteString);
    
    NSString *str = request.URL.absoluteString;
    NSRange range = [strrangeOfString:@"dswq:///"];
    NSUInteger location = range.location;
    if (location !=NSNotFound) {
        NSString *method = [strsubstringFromIndex:location + range.length];
        SEL sel =NSSelectorFromString(method);
        [selfperformSelector:sel];
    }
    
    returnYES;
}


//实现访问相册
- (void)openCamera{
    UIImagePickerController *pickerVC = [[UIImagePickerControlleralloc] init];
    pickerVC.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
    [selfpresentViewController:pickerVC animated:YEScompletion:nil];
}



2.oc调用js代码


- (void)viewDidLoad {
    [superviewDidLoad];
    
   self.webView.scrollView.hidden =YES;
   self.webView.backgroundColor = [UIColorgrayColor];
    
   // 加载一个网页
   NSURL *url = [NSURLURLWithString:@"http://www.xianhua.cn/m/"];
   NSURLRequest *request = [NSURLRequestrequestWithURL:url];
    [self.webViewloadRequest:request];
}
#pragma mark - <UIWebViewDelegate>
-(void)webViewDidFinishLoad:(UIWebView *)webView{
   // 删除底部的广告
   NSString *str = @" document.getElementsByClassName('detail_btns2')[0].remove();";
    [webViewstringByEvaluatingJavaScriptFromString:str];
    
   // 修改标题
   NSString *str2 = @"document.getElementsByTagName('h1')[0].innerText = '--鲜花网--';";
    [webViewstringByEvaluatingJavaScriptFromString:str2];
    
   // 修改底部的标题
   NSString *str3 = @"document.getElementById('xiazaiapp').getElementsByTagName('a')[0].innerText = '--下载鲜花网APP--';";
    [webViewstringByEvaluatingJavaScriptFromString:str3];
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值