(0085)iOS开发之OC与JS交互高级用法(JavaScriptCore)

前述:JavaScriptCore你不知道的OC与JS之间交互。OC与JS之间用model实现交互、通讯、传值!好玩!

几乎三年来一直断断续续接触OC与JS交互,每次觉得UIWebView OC与JS的交互已经很熟练了,但是每次研究它都会有新的收获。也越来越感觉JS和OC交互原来这么顺滑,越来越无感!完全可以像OC一样使用OC的对象,属性,方法!真心是太方便了!


##其实之前我也总结过OC与JS交互无非三种情况:WebViewJavascriptBridge、JavaScriptCore、拦截URL。##

有人问我?“我想给jsmodel中 让h5返回一堆参数可以不?”
根据这个问题?我就动手实践了一下!这里使用的JavaScriptCore的JSExport 实现的!

实践代码:https://github.com/muyushifang07/jsCallOCModel

第一部分:

//
//  ELLiveJSModel.h
//  JavaScriptCoreDemo
//
//  Created by giifidev on 2018/8/21.
//  Copyright © 2018年 reborn. All rights reserved.
//

#import 
#import 

@class ELLiveJSModel;
@protocol MyPointExports 

@property (nonatomic,assign) double x;
@property (nonatomic,assign) double y;

- (NSString *)description;
- (ELLiveJSModel *)makePointX:(double)x PointY:(double)y;

@end

@interface ELLiveJSModel : NSObject

@property (nonatomic,assign) double x;
@property (nonatomic,assign) double y;

- (NSString *)description;
- (ELLiveJSModel *)makePointX:(double)x PointY:(double)y;

@end

//
//  ELLiveJSModel.m
//  JavaScriptCoreDemo
//
//  Created by giifidev on 2018/8/21.
//  Copyright © 2018年 reborn. All rights reserved.
//

#import "ELLiveJSModel.h"

@implementation ELLiveJSModel

- (NSString *)description {
    // self.x = 7;
    // self.y = 9;
    // 这里可以看到前端赋的值20,前端直接可以操作OC 的model以及属性
    NSLog(@"打印model的X的值-----:%f",self.x);
    return @"description";
}

- (ELLiveJSModel *)makePointX:(double)x PointY:(double)y {
    ELLiveJSModel *model = [[ELLiveJSModel alloc]init];
    model.x = x;
    model.y = y;
    NSLog(@"返回一个新的model.x:%f model.y:%f",model.x,model.y);
    return model;
}

@end

第二部分:##

//
//  JSModelViewController.m
//  JavaScriptCoreDemo
//
//  Created by giifidev on 2018/8/21.
//  Copyright © 2018年 reborn. All rights reserved.
//

#import "JSModelViewController.h"
#import "ELLiveJSModel.h"

#define kScreenWidth                [UIScreen mainScreen].bounds.size.width
#define kScreenHeight               [UIScreen mainScreen].bounds.size.height

@interface JSModelViewController ()
@property(nonatomic, strong) UIWebView *webView;
@property(nonatomic, strong) JSContext *context;
@end

@implementation JSModelViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"indexModel";
    self.view.backgroundColor = [UIColor whiteColor];

    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    [self.view addSubview:self.webView];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"indexModel" ofType:@"html"];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]];
    self.webView.delegate = self;
    [self.webView loadRequest:request];
}

#pragma mark - UIWebViewDelegate

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    // 获取html title设置导航栏 title
    self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    // 捕捉异常回调
    self.context.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
        context.exception = exceptionValue;
        NSLog(@"异常信息: %@",exceptionValue);
    };

    // model 的方法JS与OC交互
    ELLiveJSModel *jsModel = [ELLiveJSModel new];
    self.context[@"testobject"] = jsModel;

    // 注意:jsModel 是在OC的对象名字,testobject 是在JS中的对象名字

    // 模拟JS调用方法
    NSString *jsCallOCModelFunction = @"testobject.description()";
    JSValue *valu = [self.context evaluateScript:jsCallOCModelFunction];
    NSLog(@"vali----:%@",[valu toString]);  // 返回值

    NSLog(@"尚未赋值X的log:%f",jsModel.x);

    // 模拟JS调用方法 给testobject.x赋值
    NSString *jsCallOCModelFunction1 = @"testobject.x=30";
    [self.context evaluateScript:jsCallOCModelFunction1];
    NSLog(@"jsCallOCFunction1----%f",jsModel.x);

    // JS双参数调用的例子
    NSString *jsCallOCModelFunction2 = @"testobject.makePointXPointY('20','50')";
    [self.context evaluateScript:jsCallOCModelFunction2];
    NSLog(@"jsCallOCFunction2----%f",jsModel.x);
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"error == %@",error);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

测试入口

(0085)iOS开发之OC与JS交互高级用法(JavaScriptCore)_第1张图片

JS修改OC的model 的属性的值按钮##

(0085)iOS开发之OC与JS交互高级用法(JavaScriptCore)_第2张图片

控制台log 可以得出 实践结果:

JS 中 可以拿到OC 的对象,并且可以操作OC 的model 的属性,也可以调用model的方法!并且JS 和 OC 操作的是同一个model 对象! 欧了!

喜欢的童鞋可以拿去研究吧!其乐无穷!

你可能感兴趣的:(iOS开发笔记)