博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OC与JS互相调用
阅读量:6848 次
发布时间:2019-06-26

本文共 4201 字,大约阅读时间需要 14 分钟。

近期项目中要用到html5来实现。涉及到OC调用JS,以及JS调用OC的方法。这里把遇到的问题以及实现方法介绍一下。

////  ViewController.h//  OC_And_JS////  Created by 张杰 on 15/7/9.//  Copyright © 2015年 张杰. All rights reserved.//#import 
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *oc_call_js_no_params;@property (weak, nonatomic) IBOutlet UIButton *oc_call_js_has_params;@property (weak, nonatomic) IBOutlet UIWebView *mWebView;@property (weak, nonatomic) IBOutlet UILabel *js_call_oc_show;- (IBAction)ocCallJsNoParams:(id)sender;- (IBAction)ocCallJsHasParams:(id)sender;@end

////  ViewController.m//  OC_And_JS////  Created by 张杰 on 15/7/9.//  Copyright © 2015年 张杰. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    _mWebView.delegate = self;        //打开URL    NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];    [self.mWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath: path]]];}- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{    NSString *urlstr = request.URL.absoluteString;    NSRange range = [urlstr rangeOfString:@"ios://jwzhangjie"];    if (range.length!=0) {        _js_call_oc_show.text = [NSString stringWithFormat:@"请訪问地址:%@", urlstr];    }    return YES;}-(void)webView:(nonnull UIWebView *)webView didFailLoadWithError:(nullable NSError *)error{    NSLog(@"载入失败");}-(void)webViewDidStartLoad:(nonnull UIWebView *)webView{    NSLog(@"開始载入");}-(void)webViewDidFinishLoad:(nonnull UIWebView *)webView{    NSLog(@"開始结束");//    对于调用js的时候最好这种方法里面或者之后}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (IBAction)ocCallJsNoParams:(id)sender {    NSString *js = [NSString stringWithFormat:@"ocCallJsNoParamsFunction();"];    [self.mWebView stringByEvaluatingJavaScriptFromString:js];}- (IBAction)ocCallJsHasParams:(id)sender {    NSString *js = [NSString stringWithFormat:@"ocCallJsHasParamsFunction('%@','%@');",@"jwzhangjie",@"http://jwzhangjie.cn"];    [self.mWebView stringByEvaluatingJavaScriptFromString:js];}@end
function ocCallJsNoParamsFunction(){    alert("OC调用JS中的无參方法");    var e = document.getElementById("js_shouw_text");    e.options.add(new Option("OC调用JS中的无參方法", 2));}function ocCallJsHasParamsFunction(name, url){    alert(name+"的博客地址为:"+url);    var e = document.getElementById("js_shouw_text");    e.options.add(new Option("OC调用JS中的有參方法", 2));}
    
OC与JS互相调用
规避1:对于OC去调用JS内容最好在
webViewDidFinishLoad方法里或者之后

规避2:在html里面引用js或者css的时候src不要带有路径,由于安装后文件都在同级文件夹以下

规避3:OC调用JS的规范

NSString *js = [NSString stringWithFormat:@"ocCallJsHasParamsFunction('%@','%@');",@"jwzhangjie",@"http://jwzhangjie.cn"];    [self.mWebView stringByEvaluatingJavaScriptFromString:js];
规避4:JS调用OC。这里通过html里面发送一个请求,然后在ios中使用
shouldStartLoadWithRequest拦截请求。依据请求url的不同进行处理。

function js_call_oc()        {            var iFrame;            iFrame = document.createElement("iframe");            iFrame.setAttribute("src", "ios://jwzhangjie");            iFrame.setAttribute("style", "display:none;");            iFrame.setAttribute("height", "0px");            iFrame.setAttribute("width", "0px");            iFrame.setAttribute("frameborder", "0");            document.body.appendChild(iFrame);            // 发起请求后这个iFrame就没用了。所以把它从dom上移除掉            iFrame.parentNode.removeChild(iFrame);            iFrame = null;        }
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{    NSString *urlstr = request.URL.absoluteString;    NSRange range = [urlstr rangeOfString:@"ios://jwzhangjie"];    if (range.length!=0) {        _js_call_oc_show.text = [NSString stringWithFormat:@"请訪问地址:%@", urlstr];    }    return YES;}
源代码地址:

http://jwzhangjie.cn/forum.php?mod=viewthread&tid=3&page=1&extra=#pid3

转载地址:http://meoul.baihongyu.com/

你可能感兴趣的文章
Android fill_parent、wrap_content和match_parent的区别
查看>>
mysql笔记1------基础
查看>>
VC++ 中简单操作MP3音乐的方法,小结
查看>>
关于 BCSCTL1 = CALBC1_12MHZ;DCOCTL = CALDCO_12MHZ; 的疑问
查看>>
启动虚拟机时提示我已移动或我已复制选项的详解
查看>>
I.MX6 make menuconfig OTG to slave only mode
查看>>
代码抽象层次2
查看>>
Linux7 下重新安装YUM
查看>>
程序员修炼之道:从小工到专家
查看>>
WPF x名称空间
查看>>
【Quartz】【程序目录结构】/DetectNonWorkingDay/src/main/config/log4j.properties
查看>>
mac OS下显示.htaccess等隐藏文件
查看>>
北大校赛2011 Word Ladder(C题) 解题报告
查看>>
OsWorkFlow学习(FSM:有限状态机)
查看>>
根据二叉树的中序遍历和层次遍历还原二叉树
查看>>
paypal支付说明
查看>>
shell 命令 文件查看ls,复制cp,移动mv,查看文件内容cat more less,查看文件信息 file...
查看>>
ADO.NET调用存储过程
查看>>
安装UBUNTU Server 11.10
查看>>
MVVM架构~使用boxy和knockoutjs实现编辑功能
查看>>