zoukankan      html  css  js  c++  java
  • OC与JS交互之JavaScriptCore

    本文摘抄自:https://hjgitbook.gitbooks.io/ios/content/04-technical-research/04-javascriptcore-note.html

    JavaScriptCore初探

    注:JavaScriptCore API也可以用Swift来调用,本文用Objective-C来介绍。

    在iOS7之前,原生应用和Web应用之间很难通信。如果你想在iOS设备上渲染HTML或者运行JavaScript,你不得不使用UIWebView。iOS7引入了JavaScriptCore,功能更强大,使用更简单。

    JavaScriptCore介绍

    JavaScriptCore是封装了JavaScript和Objective-C桥接的Objective-C API,只要用很少的代码,就可以做到JavaScript调用Objective-C,或者Objective-C调用JavaScript。

    在之前的iOS版本,你只能通过向UIWebView发送stringByEvaluatingJavaScriptFromString:消息来执行一段JavaScript脚本。并且如果想用JavaScript调用Objective-C,必须打开一个自定义的URL(例如:foo://),然后在UIWebView的delegate方法webView:shouldStartLoadWithRequest:navigationType中进行处理。

    然而现在可以利用JavaScriptCore的先进功能了,它可以:

    • 运行JavaScript脚本而不需要依赖UIWebView 
    • 使用现代Objective-C的语法(例如Blocks和下标)
    • 在Objective-C和JavaScript之间无缝的传递值或者对象
    • 创建混合对象(原生对象可以将JavaScript值或函数作为一个属性)

    使用Objective-C和JavaScript结合开发的好处:

    • 快速的开发和制作原型
      如果某块区域的业务需求变化的非常频繁,那么可以用JavaScript来开发和制作原型,这比Objective-C效率更高。 
    • 团队职责划分
      这部分参考原文吧
      Since JavaScript is much easier to learn and use than Objective-C (especially if you develop a nice JavaScript sandbox), it can be handy to have one team of developers responsible for the Objective-C “engine/framework”, and another team of developers write the JavaScript that uses the “engine/framework”. Even non-developers can write JavaScript, so it’s great if you want to get designers or other folks on the team involved in certain areas of the app.
    • JavaScript是解释型语言
      JavaScript是解释运行的,你可以实时的修改JavaScript代码并立即看到结果。
    • 逻辑写一次,多平台运行
      可以把逻辑用JavaScript实现,iOS端和Android端都可以调用

    JavaScriptCore概述

    JSValue: 代表一个JavaScript实体,一个JSValue可以表示很多JavaScript原始类型例如boolean, integers, doubles,甚至包括对象和函数。
    JSManagedValue: 本质上是一个JSValue,但是可以处理内存管理中的一些特殊情形,它能帮助引用技术和垃圾回收这两种内存管理机制之间进行正确的转换。
    JSContext: 代表JavaScript的运行环境,你需要用JSContext来执行JavaScript代码。所有的JSValue都是捆绑在一个JSContext上的。
    JSExport: 这是一个协议,可以用这个协议来将原生对象导出给JavaScript,这样原生对象的属性或方法就成为了JavaScript的属性或方法,非常神奇。
    JSVirtualMachine: 代表一个对象空间,拥有自己的堆结构和垃圾回收机制。大部分情况下不需要和它直接交互,除非要处理一些特殊的多线程或者内存管理问题。

    JSContext / JSValue

    JSVirtualMachine为JavaScript的运行提供了底层资源,JSContext为JavaScript提供运行环境,通过

    - (JSValue *)evaluateScript:(NSString *)script;
    

    方法就可以执行一段JavaScript脚本,并且如果其中有方法、变量等信息都会被存储在其中以便在需要的时候使用。 而JSContext的创建都是基于JSVirtualMachine:

    - (id)initWithVirtualMachine:(JSVirtualMachine *)virtualMachine;
    

    如果是使用- (id)init;进行初始化,那么在其内部会自动创建一个新的JSVirtualMachine对象然后调用前边的初始化方法。

    创建一个 JSContext 后,可以很容易地运行 JavaScript 代码来创建变量,做计算,甚至定义方法:

    JSContext *context = [[JSContext alloc] init];
    [context evaluateScript:@"var num = 5 + 5"];
    [context evaluateScript:@"var names = ['Grace', 'Ada', 'Margaret']"];
    [context evaluateScript:@"var triple = function(value) { return value * 3 }"];
    JSValue *tripleNum = [context evaluateScript:@"triple(num)"];
    

    任何出自 JSContext 的值都被可以被包裹在一个 JSValue 对象中,JSValue 包装了每一个可能的 JavaScript 值:字符串和数字;数组、对象和方法;甚至错误和特殊的 JavaScript 值诸如 null 和 undefined。

    可以对JSValue调用toStringtoBooltoDoubletoArray等等方法把它转换成合适的Objective-C值或对象。

    Objective-C调用JavaScript

    例如有一个"Hello.js"文件内容如下:

    function printHello() {
    
    }
    

    在Objective-C中调用printHello方法:

    NSString *scriptPath = [[NSBundle mainBundle] pathForResource:@"hello" ofType:@"js"];
    NSString *scriptString = [NSString stringWithContentsOfFile:scriptPath encoding:NSUTF8StringEncoding error:nil];
    
    JSContext *context = [[JSContext alloc] init];
    [context evaluateScript:scriptString];
    
    JSValue *function = self.context[@"printHello"];
    [function callWithArguments:@[]];
    

    分析以上代码:

    首先初始化了一个JSContext,并执行JavaScript脚本,此时printHello函数并没有被调用,只是被读取到了这个context中。

    然后从context中取出对printHello函数的引用,并保存到一个JSValue中。

    注意这里,从JSContext中取出一个JavaScript实体(值、函数、对象),和将一个实体保存到JSContext中,语法均与NSDictionary的取值存值类似,非常简单。

    最后如果JSValue是一个JavaScript函数,可以用callWithArguments来调用,参数是一个数组,如果没有参数则传入空数组@[]。

    JavaScript调用Objective-C

    还是上面的例子,将"hello.js"的内容改为:

    function printHello() {
        print("Hello, World!");
    }
    

    这里的print函数用Objective-C代码来实现

    NSString *scriptPath = [[NSBundle mainBundle] pathForResource:@"hello" ofType:@"js"];
    NSString *scriptString = [NSString stringWithContentsOfFile:scriptPath encoding:NSUTF8StringEncoding error:nil];
    
    JSContext *context = [[JSContext alloc] init];
    [context evaluateScript:scriptString];
    
    self.context[@"print"] = ^(NSString *text) {
        NSLog(@"%@", text");
    };
    
    JSValue *function = self.context[@"printHello"];
    [function callWithArguments:@[]];
    

    这里将一个Block以"print"为名传递给JavaScript上下文,JavaScript中调用print函数就可以执行这个Objective-C Block。

    注意这里JavaScript中的字符串可以无缝的桥接为NSString,实参"Hello, World!"被传递给了NSString类型的text形参。

    异常处理

    当JavaScript运行时出现异常,会回调JSContext的exceptionHandler中设置的Block

    context.exceptionHandler = ^(JSContext *context, JSValue *exception) {
          NSLog(@"JS Error: %@", exception);
    };
    
    [context evaluateScript:@"function multiply(value1, value2) { return value1 * value2 "];
    // 此时会打印Log "JS Error: SyntaxError: Unexpected end of script"
    

    JSExport

    JSExport是一个协议,可以让原生类的属性或方法称为JavaScript的属性或方法。

    看下面的例子:

    @protocol ItemExport <JSExport>
    @property (strong, nonatomic) NSString *name;
    @property (strong, nonatomic) NSString *description;
    @end
    
    @interface Item : NSObject <ItemExport>
    @property (strong, nonatomic) NSString *name;
    @property (strong, nonatomic) NSString *description;
    @end
    

    注意Item类不去直接符合JSExport,而是符合一个自己的协议,这个协议去继承JSExport协议。

    例如有如下JavaScript代码

    function Item(name, description) {
        this.name = name;
        this.description = description;
    }
    
    var items = [];
    
    function addItem(item) {
        items.push(item);
    }
    

    可以在Objective-C中把Item对象传递给addItem函数

    Item *item = [[Item alloc] init];
    item.name = @"itemName";
    item.description = @"itemDescription";
    
    JSValue *function = context[@"addItem"];
    [function callWithArguments:@[item]];
    

    或者把Item类导出到JavaScript环境,等待稍后使用

    [self.context setObject:Item.self forKeyedSubscript:@"Item"];
    

    内存管理陷阱

    Objective-C的内存管理机制是引用计数,JavaScript的内存管理机制是垃圾回收。在大部分情况下,JavaScriptCore能做到在这两种内存管理机制之间无缝无错转换,但也有少数情况需要特别注意。

    在block内捕获JSContext

    Block会为默认为所有被它捕获的对象创建一个强引用。JSContext为它管理的所有JSValue也都拥有一个强引用。并且,JSValue会为它保存的值和它所在的Context都维持一个强引用。这样JSContext和JSValue看上去是循环引用的,然而并不会,垃圾回收机制会打破这个循环引用。

    看下面的例子:

    self.context[@"getVersion"] = ^{
        NSString *versionString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    
        versionString = [@"version " stringByAppendingString:versionString];
    
        JSContext *context = [JSContext currentContext]; // 这里不要用self.context
        JSValue *version = [JSValue valueWithObject:versionString inContext:context];
    
        return version;
    };
    

    使用[JSContext currentContext]而不是self.context来在block中使用JSContext,来防止循环引用。

    JSManagedValue

    当把一个JavaScript值保存到一个本地实例变量上时,需要尤其注意内存管理陷阱。 用实例变量保存一个JSValue非常容易引起循环引用。

    看以下下例子,自定义一个UIAlertView,当点击按钮时调用一个JavaScript函数:

    #import <UIKit/UIKit.h>
    #import <JavaScriptCore/JavaScriptCore.h>
    
    @interface MyAlertView : UIAlertView
    
    - (id)initWithTitle:(NSString *)title
                message:(NSString *)message
                success:(JSValue *)successHandler
                failure:(JSValue *)failureHandler
                context:(JSContext *)context;
    
    @end
    

    按照一般自定义AlertView的实现方法,MyAlertView需要持有successHandler,failureHandler这两个JSValue对象

    向JavaScript环境注入一个function

    self.context[@"presentNativeAlert"] = ^(NSString *title,
                                            NSString *message,
                                            JSValue *success,
                                            JSValue *failure) {
       JSContext *context = [JSContext currentContext];
       MyAlertView *alertView = [[MyAlertView alloc] initWithTitle:title 
                                                           message:message
                                                           success:success
                                                           failure:failure
                                                           context:context];
       [alertView show];
    };
    

    因为JavaScript环境中都是“强引用”(相对Objective-C的概念来说)的,这时JSContext强引用了一个presentNativeAlert函数,这个函数中又强引用了MyAlertView 等于说JSContext强引用了MyAlertView,而MyAlertView为了持有两个回调强引用了successHandler和failureHandler这两个JSValue,这样MyAlertView和JavaScript环境互相引用了。

    所以苹果提供了一个JSMagagedValue类来解决这个问题。

    看MyAlertView.m的正确实现:

    #import "MyAlertView.h"
    
    @interface XorkAlertView() <UIAlertViewDelegate>
    @property (strong, nonatomic) JSContext *ctxt;
    @property (strong, nonatomic) JSMagagedValue *successHandler;
    @property (strong, nonatomic) JSMagagedValue *failureHandler;
    @end
    
    @implementation MyAlertView
    
    - (id)initWithTitle:(NSString *)title
                message:(NSString *)message
                success:(JSValue *)successHandler
                failure:(JSValue *)failureHandler
                context:(JSContext *)context {
    
        self = [super initWithTitle:title
                        message:message
                       delegate:self
              cancelButtonTitle:@"No"
              otherButtonTitles:@"Yes", nil];
    
        if (self) {
            _ctxt = context;
    
            _successHandler = [JSManagedValue managedValueWithValue:successHandler];
            // A JSManagedValue by itself is a weak reference. You convert it into a conditionally retained
            // reference, by inserting it to the JSVirtualMachine using addManagedReference:withOwner:
            [context.virtualMachine addManagedReference:_successHandler withOwner:self];
    
            _failureHandler = [JSManagedValue managedValueWithValue:failureHandler];
            [context.virtualMachine addManagedReference:_failureHandler withOwner:self];
        }
        return self;
    }
    
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        if (buttonIndex == self.cancelButtonIndex) {
            JSValue *function = [self.failureHandler value];
            [function callWithArguments:@[]];
        } else {
            JSValue *function = [self.successHandler value];
            [function callWithArguments:@[]];
        }
    
        [self.ctxt.virtualMachine removeManagedReference:_failureHandler withOwner:self];
        [self.ctxt.virtualMachine removeManagedReference:_successHandler withOwner:self];
    }    
    
    @end
    

    分析上面例子,从外部传入的JSValue对象在类内部使用JSManagedValue来保存。

    JSManagedValue本身是一个弱引用对象,需要调用JSVirtualMachine的addManagedReference:withOwner:把它添加到JSVirtualMachine对象中,确保使用过程中JSValue不会被释放

    当用户点击AlertView上的按钮时,根据用户点击哪一个按钮,来执行对应的处理函数,这时AlertView也随即被销毁。 这时需要手动调用removeManagedReference:withOwner:来移除JSManagedValue。

    参考资料

  • 相关阅读:
    VS创建MVC出错解决方法
    服务器环境~某个页面无法访问的处理
    SSIS 处理错误的方法
    SSIS 数据流的执行树和数据管道
    利用SSIS的ForcedExecutionResult 属性 和CheckPoint调试Package
    处于同一域中的两台SQL Server 实例无法连接
    SQL Server 日期和时间类型
    Lookup 转换组件
    约束2:主键约束,唯一约束和唯一索引
    查询语句影响的行数
  • 原文地址:https://www.cnblogs.com/yueyuanyueyuan/p/5651266.html
Copyright © 2011-2022 走看看