OC-Js的交互问题

来源:互联网 发布:海马模拟器 for mac 编辑:程序博客网 时间:2024/06/16 22:02

 最近空闲,把以前接触到的关于JS与OC之间是如何交互的给大家总结一下,同时附上网页端的代码,让大家看起来轻松明了!

1、iOS端需要处理的事情

先添加javascriptcore.framework的系统库

#import <JavaScriptCore/JavaScriptCore.h>添加头文件

-(void)makeWebView

{

    UIWebView *webView = [[UIWebViewalloc]initWithFrame:CGRectMake(0,64,SCREEN_WIDTH,SCREEN_HEIGHT-64)];

    webView.delegate=self;

    [self.viewaddSubview:webView];

    [SVProgressHUDshowWithStatus:@"正在加载中……"maskType:SVProgressHUDMaskTypeBlack];

    self.href=[@"http://"stringByAppendingString:self.href];

    //准备webView的属性

    NSURLRequest *req = [NSURLRequestrequestWithURL:[NSURLURLWithString:self.href]];

       [webView loadRequest:req];

    webView.scalesPageToFit =YES;//让页面大小适配

    JSContext *context = [webViewvalueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

 context[@"userToAppointment"] = ^() {

     //项目名称、医生名字、预约价格、商品id

          NSArray *args = [JSContextcurrentArguments];

            AppointOrderViewController * appointOrder = [[AppointOrderViewController alloc]initWithNibName:@"AppointOrderViewController"bundle:nil];

            appointOrder.projectName =[NSStringstringWithFormat:@"%@",args[0]];//不转成字符串的话,给控件“Lable”赋值时会为空

            appointOrder.appointDoctor = [NSStringstringWithFormat:@"%@",args[1]];

            appointOrder.appointPrice =[NSStringstringWithFormat:@"%@",args[2]];

            appointOrder.projectID = [NSStringstringWithFormat:@"%@",args[3]];

            appointOrder.shopName=[NSStringstringWithFormat:@"%@",args[0]];

            [self.navigationControllerpushViewController:appointOrderanimated:YES];      

    };

context[@"doctorPage"]=^(){

     //医生id

        NSArray *doctorIdArr = [JSContext currentArguments];

       DoctorHomePageViewController * docHomePage = [[DoctorHomePageViewController alloc] initWithNibName:@"DoctorHomePageViewController" bundle:nil];

        docHomePage.doctID =doctorIdArr[0];

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

    };


}

2、网页端的代码

2.1点击网页的按钮调用网页中自己的方法:

 
2.2在网页调用自己的方法的实现中调用与网页端商定好的OC的方法,并进行传值

0 0
原创粉丝点击