码迷,mamicode.com
首页 > Web开发 > 详细

OC与JS的交互

时间:2017-03-07 12:44:34      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:_id   gre   efault   文件中   anim   code   tle   conf   share   

关于oc与js的交互,本人用的是原生类

JSExport

实现。

 

本人不擅长文字描述,故直接上代码

1,首先创建一个继承 NSObject的类,创建代理,实现代理方法,.h文件

 1 #import <Foundation/Foundation.h>
 2 #import <JavaScriptCore/JavaScriptCore.h>
 3 @protocol JSObjcDelegate1 <JSExport>
 4 - (void)getUser:(NSString *)callString;
 5 - (void)startNewWeb:(NSString *)callString;
 6 - (void)getCall:(NSString *)callString;
 7 -(void)goTojifenVC;
 8 -(void)startMyGife;
 9 -(void)showDialog:(NSString *)jsonString;
10 //多参数的时候,第二个参数外部名首字母都要大写-(void)showDialog:(NSString *)imgUrl Second:(NSString *)shareUrl Thrid:(NSString *)title Contenxt:(NSString *)contenct;
11 -(void)call;
12 @end
13 
14 @interface JSObjcDelegate_VC : NSObject<JSObjcDelegate1,UMSocialUIDelegate>
15 @property(nonatomic,strong)UIViewController *vcs;
16 @property(nonatomic,strong)FenXaing_Vc *shareView;;
17 @property(nonatomic,assign)int a;
18 @end

 

在.m文件中实现代理方法,此中以自己的方法为例

  1 #import "JSObjcDelegate_VC.h"
  2 #import "QianDao_jilu_VC.h"
  3 #import "LoginGetMsgNumResult.h"
  4 #import "MY_FreeUse_VC.h"
  5 #import "Mine_jifenduihuan_VC.h"
  6 #import "Json_Model.h"
  7 @implementation JSObjcDelegate_VC
  8 
  9 - (void)getUser:(NSString *)callString{
 10     
 11         NSLog(@"id%@",callString);
 12         //注册成功,保存用户个人信息。
 13         NSData *jsonData = [callString dataUsingEncoding:NSUTF8StringEncoding];
 14         NSError *err;
 15         NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
 16         LoginGetMsgNumResult *result = [LoginGetMsgNumResult mj_objectWithKeyValues:dic];
 17         VGAccount *account = [[VGAccount alloc] init];
 18         VGUser *user = result.data;
 19         account.user_id = user.user_id;
 20         account.user_phone = user.user_phone;
 21         account.user_nickname = user.user_nickname;
 22         account.user_autograph = user.user_autograph;
 23         account.user_image = user.user_image;
 24         account.user_sex = user.user_sex;
 25         account.user_vip = user.user_vip;
 26         account.user_harvest = user.user_harvest;
 27         account.is_order = user.is_order;
 28         account.card_status = user.card_status;
 29         account.address_status = user.address_status;
 30         account.user_birthdate = user.user_birthdate;
 31         [VGAccountTool save:account];
 32         appDelegate.account = account;
 33 }
 34 
 35 //签到记录
 36 - (void)startNewWeb:(NSString *)callString{
 37     //[_vcs stopLoading];
 38     NSLog(@"%@",callString);
 39     QianDao_jilu_VC *vc=[[QianDao_jilu_VC alloc]init];
 40     vc.url=callString;
 41     if (self.a==1) {
 42         [_vcs.navigationController presentViewController:vc animated:YES completion:nil];
 43     }else{
 44         [_vcs.navigationController pushViewController:vc animated:YES];
 45     }
 46 
 47 }
 48 
 49 
 50 - (void)getCall:(NSString *)callString{
 51     NSLog(@"id:%@", callString);
 52     //跳转页面
 53     Prodetail_VC *vc=[[Prodetail_VC alloc]init];
 54     vc.ids=callString;
 55     [CommonTool setTabBarHiden:YES];
 56     
 57     [_vcs.navigationController pushViewController:vc animated:YES];
 58 }
 59 
 60 
 61 -(void)goTojifenVC{
 62     Mine_jifenduihuan_VC *vc=[[Mine_jifenduihuan_VC alloc]init];
 63     [CommonTool setTabBarHiden:YES];
 64     [_vcs.navigationController pushViewController:vc animated:YES];
 65 
 66 }
 67 
 68 -(void)startMyGife{
 69     MY_FreeUse_VC *vc=[[MY_FreeUse_VC alloc]init];
 70     [CommonTool setTabBarHiden:YES];
 71     [_vcs.navigationController pushViewController:vc animated:YES];
 72 }
 73 
 74 //分享
 75 -(void)showDialog:(NSString *)jsonString{
 76     NSLog(@"%@",jsonString);
 77     NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
 78     NSError *err;
 79     NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
 80     
 81     Json_Model *model=[Json_Model mj_objectWithKeyValues:dic];
 82     NSLog(@"%@",model.title);
 83     
 84     [_shareView setShareVC:_vcs content:model.contenxt image:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.imgUrl]]] url:model.url price:nil title:model.title];
 85     
 86     [_shareView show];
 87     
 88     
 89     /*
 90     
 91     [UMSocialData defaultData].extConfig.qqData.title =model.title;
 92     [UMSocialData defaultData].extConfig.qqData.url = model.url;
 93     [UMSocialData defaultData].extConfig.qzoneData.title =model.title;
 94     [UMSocialData defaultData].extConfig.qzoneData.url = model.url;
 95     
 96     [UMSocialData defaultData].extConfig.wechatTimelineData.title =model.title;
 97        [UMSocialData defaultData].extConfig.wechatTimelineData.url = model.url;
 98     [UMSocialData defaultData].extConfig.wechatSessionData.title =model.title;
 99     [UMSocialData defaultData].extConfig.wechatSessionData.url = model.url;
100 
101     [UMSocialSnsService presentSnsIconSheetView:_vcs appKey:nil shareText:model.contenxt shareImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.imgUrl]]] shareToSnsNames:@[UMShareToQQ, UMShareToQzone, UMShareToWechatSession, UMShareToWechatTimeline,UMShareToSina] delegate:self];
102   
103      */
104 }
105 
106 
107 -(void)call{
108     NSLog(@"sssssssss");
109 }
110 @end

3.在需要的webView中,我们这可以这样写

 1 #import "QianDaoViewController.h"
 2 #import "QianDao_jilu_VC.h"
 3 #import "LoginGetMsgNumResult.h"
 4 #import "JSObjcDelegate_VC.h"
 5 @interface QianDaoViewController ()
 6 @property(nonatomic,strong) NSURLRequest* request1;
 7 @end
 8 
 9 @implementation QianDaoViewController
10 -(void)viewWillAppear:(BOOL)animated{
11     [super viewWillAppear:animated];
12     [MobClick beginLogPageView:@"首页 - 签到"];
13     
14     
15 }
16 
17 -(void)viewWillDisappear:(BOOL)animated{
18     [super viewWillDisappear:animated];
19     [MobClick endLogPageView:@"首页 - 签到"];
20 }
21 
22 - (void)viewDidLoad {
23     [super viewDidLoad];
24     self.view.backgroundColor=[UIColor whiteColor];
25     
26     self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64)];
27     self.webView.delegate = self;
28     NSString *str1;
29     if (appDelegate.account.user_id.length>0) {
30         str1=appDelegate.account.user_id;
31     }else{
32         str1=@"";
33     }
34     NSString *str=[NSString stringWithFormat:@"%@&sign=%@",self.URLString,str1];
35     NSLog(@"%@",str);
36     NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:str]];
37     
38     self.request1=request;
39     [self.webView loadRequest:request];
40     
41     [self.view addSubview:self.webView];
42 
43 }
44 
45 
46 
47 #pragma mark - UIWebViewDelegate
48 - (void)webViewDidFinishLoad:(UIWebView *)webView {
49     [self.view hideHUD];
50     // js的弹窗是阻塞式的!
51     //    [webView stringByEvaluatingJavaScriptFromString:@"clickme();"];
52     
53     NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
54     self.title=title;
55    // NSLog(@"%@", title);
56     
57     JSContext *jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
58     JSObjcDelegate_VC *jsHandler = [JSObjcDelegate_VC new];
59     jsHandler.vcs=self;
60     //jsHandler.delegate=self;
61     //使jsContext可以识别javascript这个类的方法
62     jsContext[@"tianbai"] = jsHandler;
63     //NSString *jsStr2=@"tianbai.startNewWeb(‘参数1‘)";
64    // [jsContext evaluateScript:jsStr2];
65     /*
66     self.jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
67     self.jsContext[@"tianbai"] = self;
68     self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
69         context.exception = exceptionValue;
70         NSLog(@"异常信息:%@", exceptionValue);
71     };
72     */
73 }
74 
75 
76 -(void)webViewDidStartLoad:(UIWebView *)webView
77 {
78     [self.view showHUD];
79 }
80 @end

 

OC与JS的交互

标签:_id   gre   efault   文件中   anim   code   tle   conf   share   

原文地址:http://www.cnblogs.com/henusyj-1314/p/6513944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!