JS调用OC方法(UIWebView)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012581760/article/details/84970734
#import "KURLViewController.h"
#import "CommonWebView.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import <WebKit/WebKit.h>
#import "LiveMemberModel.h"

@protocol TestJSCallOCProtocol <JSExport>
- (void)jsCallOC:(NSString *)firstPar checkMember:(NSString *)memberStr;
- (void)jsCallOC:(NSString *)firstPar checkDept:(NSString *)deptStr;
- (void)goBack;
@end
@interface KURLViewController ()<CommonWebViewDelegate,TestJSCallOCProtocol>
@property (nonatomic, strong) CommonWebView *myWebView;
@property (nonatomic, strong) JSContext *jsContext;

@end

@implementation KURLViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _myWebView = [[CommonWebView alloc]initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT) parentViewController:self URL:self.urlStr];
    _myWebView.webViewDelegate = self;
    [self.view addSubview:self.myWebView];
    self.navigationItem.leftBarButtonItem = [self leftItemBtn];
}

- (UIBarButtonItem *)leftItemBtn
{
    UIButton *button = [self mLeftBtn];
    [button addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    return  [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didFinishLoadURLDescription:(NSString *)urlDescription
{
    self.jsContext = [self.myWebView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    [self.jsContext setObject:self forKeyedSubscript:@"WebViewJavascriptBridge"];
    self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exception) {
        NSLog(@"====%@===%@==",exception,context);
    };
    NSLog(@"1111111");
}

- (void)acquireUrlWithJSMember{
    JSContext *context = [self.myWebView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

    NSMutableArray *array = [NSMutableArray array];
    LiveMemberModel *model = [[LiveMemberModel alloc]init];
    model.DeptId = @"58721790-0995-4f52-85d2-2b69edfb5eba";
    model.UserId = @"313b751a-7464-423e-a17d-ca2748ee77d7";
    model.OrgId = @"9995385f-653f-43f4-b26c-607360c65ae4";
    model.RealName = @"马焕菲";
    model.UserName = @"yd_mahf";
    [array addObject:model];
    NSString *str =  [array yy_modelToJSONString];
    NSString *result = [self.myWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.app.ocCallJsSelectDeptOrMember('%@')",str]];
    NSLog(@"==%@==",result);
}


- (void)acquireUrlWithJSDept
{
    NSMutableArray *array = [NSMutableArray array];
    LiveMemberModel *model = [[LiveMemberModel alloc]init];
    model.DeptId = @"58721790-0995-4f52-85d2-2b69edfb5eba";
    model.FullName = @"知识管理中心";
    model.OrgId = @"9995385f-653f-43f4-b26c-607360c65ae4";
    model.ParentId = @"9995385f-653f-43f4-b26c-607360c65ae4";
    model.Grade = @"1";
    [array addObject:model];
    
    NSString *str =  [array yy_modelToJSONString];
    //OC调用JS的方法(该方法一定要在webViewDidFinishedLoaded之后加载否则OC的webView没有加载完H5就不能够调用到js的方法)
    NSString *result =   [self.myWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.ocCallJsSelectDeptOrMember('%@')",str]];
    NSLog(@"==%@==",result);
    
}


#pragma mark - TestJSCallOCProtocol
- (void)jsCallOC:(NSString *)firstPar checkMember:(NSString *)memberStr {
    NSLog(@"111");
  
    [self acquireUrlWithJSMember];
}


- (void)jsCallOC:(NSString *)firstPar checkDept:(NSString *)deptStr
{
    NSLog(@"1111");
    [self acquireUrlWithJSDept];
}

- (void)goBack
{
    [self savePreference:@"ToDoTaskViewController" withValue:@"Refresh"];
    mDispatchAfter(YES, 2, ^{
        [self backAction];
    });
}
@end

JS脚本中调用OC的方法
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/u012581760/article/details/84970734