[转]UIWebView的Javascript运行时对象
An alternative, that may get you rejected from the app store, is to use WebScriptObject. These APIs are public on OSX but are not on iOS. You need to define interfaces to the internal classes. @interface WebScriptObject: NSObject
@end @interface WebView
- (WebScriptObject *)windowScriptObject;
@end @interface UIWebDocumentView: UIView
- (WebView *)webView;
@end
You need to define your object that's going to serve as your WebScriptObject @interface WebScriptBridge: NSObject
- (void)someEvent: (uint64_t)foo :(NSString *)bar;
- (void)testfoo;
+ (BOOL)isKeyExcludedFromWebScript:(const char *)name;
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector;
+ (WebScriptBridge*)getWebScriptBridge;
@end static WebScriptBridge *gWebScriptBridge = nil; @implementation WebScriptBridge
- (void)someEvent: (uint64_t)foo :(NSString *)bar
{
NSLog(bar);
} -(void)testfoo {
NSLog(@"testfoo!");
} + (BOOL)isKeyExcludedFromWebScript:(const char *)name;
{
return NO;
} + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector;
{
return NO;
} + (NSString *)webScriptNameForSelector:(SEL)sel
{
// Naming rules can be found at: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/WebKit/Protocols/WebScripting_Protocol/Reference/Reference.html
if (sel == @selector(testfoo)) return @"testfoo";
if (sel == @selector(someEvent::)) return @"someEvent"; return nil;
}
+ (WebScriptBridge*)getWebScriptBridge {
if (gWebScriptBridge == nil)
gWebScriptBridge = [WebScriptBridge new]; return gWebScriptBridge;
}
@end
Now set that an instance to your UIWebView if ([uiWebView.subviews count] > ) {
UIView *scrollView = uiWebView.subviews[]; for (UIView *childView in scrollView.subviews) {
if ([childView isKindOfClass:[UIWebDocumentView class]]) {
UIWebDocumentView *documentView = (UIWebDocumentView *)childView;
WebScriptObject *wso = documentView.webView.windowScriptObject; [wso setValue:[WebScriptBridge getWebScriptBridge] forKey:@"yourBridge"];
}
}
}
Now inside of your javascript you can call: yourBridge.someEvent(, "hello");
yourBridge.testfoo();
=====================================================
As a rule, iOS programmers don’t think much about JavaScript. We spend our days swimming inC and Objective-C, while occasionally dipping our toes into the waters of C++. ButJavaScript has been consistently popular for the past decade, mostly on the web—both in the browser and on the back end.
Until iOS 7, there was only one way to interact with JavaScript from an iOS app. UIWebView
exposes one method, -stringByEvaluatingJavaScriptFromString:
. A UIWebView
has its own JavaScript runtime, and you can use this simple API to evaluate arbitrary scripts, which might be calling existing functions in the HTML document that the web view is displaying. While having access to the JavaScript engine is nice, this single method API is very limiting.
Enter the JavaScriptCore framework. Its C-based API has been exposed on OS X for some time, but with iOS 7 and OS X 10.9, it has been updated and expanded with an Objective-C interface. JavaScriptCore gives developers deep access to the full JavaScript runtime from Objective-C. You can syntax-check and execute scripts, access variables, receive callbacks, and share Objective-C objects, making possible a wide range of interactions. (One caveat: on iOS, there is currently no way to access the UIWebView’s runtime, so unfortunately it’s not possible to tie into web apps at this level.)
SHARE AND SHARE ALIKE
JavaScript runs in a virtual machine, which is represented by the JSVirtualMachine
class. It’s very lightweight; the most important thing to know about it is that you can support multithreaded JavaScript by instantiating multiple JSVirtualMachine
s. Within each JSVirtualMachine
can be an arbitrary number of JSContext
s. A JSContext
talks to a JavaScript runtime environment and provides several key capabilities, two of which are especially relevant to this quick tour: access to the global object, and the ability to execute scripts.
Every variable defined in the JavaScript global context is exposed through keyed subscript access in the JSContext
. Hence you can access the contents of JavaScript variables in much the same way you would store and retrieve values in an NSDictionary. For example:
JSContext *context = [[JSContext alloc] initWithVirtualMachine:[[JSVirtualMachine alloc] init]];
context[@"a"] = @5;
At this point, the internal global object for the JavaScript execution context has a property named a whose value is 5. (To put it another way, there is a global variable a
whose value is 5; i.e. what you would get if you typed var a = 5
.) To go the other direction, you need to know about JSValue
s. The aptly named JSValue
class is the counterpart to an arbitrary piece of JavaScript data. Any JavaScript-native datatype can be expressed by a JSValue
. To get the value of a JavaScript variable, here’s what the code looks like.
JSValue *aValue = context[@"a"];
double a = [aValue toDouble];
NSLog(@"%.0f", a);
2013-09-13 14:15:56.021 JSThing[53260:a0b] 5
The value of our local Objective-C variable a
is 5.0, as you would expect. Now let’s execute a script to do something interesting. Note that you have to go back to the context to get a newJSValue
for a
. If you try printing aValue
, you’ll notice that its value is still 5.
[context evaluateScript:@"a = 10"];
JSValue *newAValue = context[@"a"];
NSLog(@"%.0f", [newAValue toDouble]);
2013-09-13 14:15:56.021 JSThing[53260:a0b] 10
After these lines of code, the variable x
(both in our Objective-C stack context and in the JavaScript runtime) is equal to 10.0. A JSValue
, much like NSString
, is immutable, so you can’t modify it in Objective-C to cause the value of the JavaScript x
to change. Likewise, you can’t change the value of the variable a
in JavaScript and expect your JSValue
to change. Each time a change is made, it has to be copied across the boundary of the two execution environments.
FUNCTIONAL EXECUTION
In order to do anything useful with your JavaScript execution environment, you’ll need to be able to actually execute JavaScript code. Unlike UIWebViews displaying web pages with scripts already defined, your newly-created JSContext
is a blank slate. Rectify that by creating a function and executing it.
[context evaluateScript:@"var square = function(x) {return x*x;}"];
JSValue *squareFunction = context[@"square"];
NSLog(@"%@", squareFunction);
JSValue *aSquared = [squareFunction callWithArguments:@[context[@"a"]]];
NSLog(@"a^2: %@", aSquared);
JSValue *nineSquared = [squareFunction callWithArguments:@[@9]];
NSLog(@"9^2: %@", nineSquared);
2013-09-13 14:22:37.597 JSThing[53327:a0b] function (x) {return x*x;}
2013-09-13 14:17:40.581 JSThing[53282:a0b] a^2: 25
2013-09-13 14:17:40.582 JSThing[53282:a0b] 9^2: 81
The -callWithArguments
method of JSValue
takes an NSArray
of arguments and will only work if the receiver is a valid JavaScript function; if you had a syntax error in your function the resulting values from -callWithArguments
would have been undefined. Note that arguments may be passed either as NSNumber
s or JSValue
s.
Functions can work the other way, too. You can pass an Objective-C block to a JSContext
and it will then behave as a function. That way, your scripts can call back into your iOS code:
context[@"factorial"] = ^(int x) {
int factorial = 1;
for (; x > 1; x--) {
factorial *= x;
}
return factorial;
};
[context evaluateScript:@"var fiveFactorial = factorial(5);"];
JSValue *fiveFactorial = context[@"fiveFactorial"];
NSLog(@"5! = %@", fiveFactorial);
2013-09-13 14:22:37.598 JSThing[53327:a0b] 5! = 120
By providing a function in this way, you can write your JavaScript code to trigger callbacks in your iOS code. There are some gotchas—you should avoid capturing references to JSValue
orJSContext
objects from within your function blocks, since those objects retain references that are likely to cause leaks.
OBJECT PERMANENCE
A JSValue
wraps all manner of JavaScript values. This includes primitives and object types, along with a few convenience methods for commonly used classes such as NSArray
. Here’s the complete list, courtesy of JSValue.h
:
// Objective-C type | JavaScript type
// --------------------+---------------------
// nil | undefined
// NSNull | null
// NSString | string
// NSNumber | number, boolean
// NSDictionary | Object object
// NSArray | Array object
// NSDate | Date object
// NSBlock | Function object
// id | Wrapper object
// Class | Constructor object
So far, you’ve passed NSNumber
s and NSBlock
s back and forth. While this is interesting and useful, notice that none of the built-in bridged types is mutable. This means that there’s no way to have a variable in Objective-C code that maps to a JavaScript value such that modifying one changes the other as well.
Or is there? Take a look at the last two lines of this table. Any object or class can be bridged to JavaScript. Any attempt to pass an object to a JSContext
which is not NSNull
, NSString
,NSNumber
, NSDictionary
, NSArray
or NSDate
will result in JavaScriptCore importing the relevant class hierarchy into the JavaScript execution context and creating equivalent classes and prototypes.
You can use the JSExport
protocol to expose parts of your custom classes to JavaScript. A wrapper object will be created on the other side, which will act as a passthrough. Thus one object can be shared between both execution contexts, read or changed by either one and consistent across both.
WRAPPING UP
Here’s a simple example of how to bridge your own class to JavaScript.
@protocol ThingJSExports <JSExport>
@property (nonatomic, copy) NSString *name;
@end @interface Thing : NSObject <ThingJSExports>
@property (nonatomic, copy) NSString *name;
@property (nonatomic) NSInteger number;
@end @implementation Thing
- (NSString *)description {
return [NSString stringWithFormat:@"%@: %d", self.name, self.number];
}
@end
By declaring that ThingJSExports
inherits from JSExport
, you mark the name property to be exposed to JavaScript. Now you can create an object that exists in both environments simultaneously. If you make changes to the Objective-C object, you will notice changes in the JavaScript value representing it. If you make changes to exposed properties on the JavaScript object, those changes will be reflected in the Objective-C object.
Thing *thing = [[Thing alloc] init];
thing.name = @"Alfred";
thing.number = 3;
context[@"thing"] = thing;
JSValue *thingValue = context[@"thing"];
Logging out the initial values produces expected results:
NSLog(@"Thing: %@", thing);
NSLog(@"Thing JSValue: %@", thingValue);
2013-09-13 14:25:48.516 JSThing[53344:a0b] Thing: Alfred: 3
2013-09-13 14:25:48.517 JSThing[53344:a0b] Thing JSValue: Alfred: 3
Now, if you change the properties of the local object, the wrapped JavaScript object changes:
thing.name = @"Betty";
thing.number = 8; NSLog(@"Thing: %@", thing);
NSLog(@"Thing JSValue: %@", thingValue);
2013-09-13 14:25:48.517 JSThing[53344:a0b] Thing: Betty: 8
2013-09-13 14:25:48.517 JSThing[53344:a0b] Thing JSValue: Betty: 8
However, the only changes that can propagate from JavaScript to Objective-C are those to the name property.
[context evaluateScript:@"thing.name = \"Carlos\"; thing.number = 5"]; NSLog(@"Thing: %@", thing);
NSLog(@"Thing JSValue: %@", thingValue);
2013-09-13 14:25:48.518 JSThing[53344:a0b] Thing: Carlos: 8
2013-09-13 14:25:48.518 JSThing[53344:a0b] Thing JSValue: Carlos: 8
(What happened when we set thing.number
to 5 in our script? Hint: try evaluating the script"thing.number"
before and after modifying it from JavaScript and see what the resultingJSValue
s are.)
This post has just scratched the surface of JavaScriptCore. Object instantiation, script syntax checking, regular expression support, and more are available either through the Objective-C API or the lower-level C API. JavaScriptCore is part of WebKit
, which is an open-source project. If you’re curious, you can poke around in the source here.
=====================================================
[转]UIWebView的Javascript运行时对象的更多相关文章
- 教你如何检查一个函数是否为JavaScript运行时环境内建函数
在开发过程中,对于某些API在现有的JavaScript运行时环境不支持的时候,我们大都会采用加入polyfill来解决这个问题.但有些时候我们可能需要知道现在某个API到底是否为运行时环境所原生支持 ...
- JAVA调用系统命令或可执行程序--返回一个Runtime运行时对象,然后启动另外一个进程来执行命令
通过 java.lang.Runtime 类可以方便的调用操作系统命令,或者一个可执行程序,下面的小例子我在windows和linux分别测试过,都通过.基本原理是,首先通过 Runtime.getR ...
- 【基本功】Java动态追踪技术探究 不重启JVM,替换掉已经加载的类?不重启JVM,获知运行时对象的属性
https://mp.weixin.qq.com/s/_hSaI5yMvPTWxvFgl-UItA 小结: 1.根据Java的类加载机制,在同一个ClassLoader中,类是不允许重复的: 2.单例 ...
- [SQL Server]无法创建 SSIS 运行时对象,请验证 DTS.dll 是否可用及是否已注册
很大可能是SQL Server Management Studio(SSMS)版本与当前操作系统不兼容造成的,与数据库本身没有关系,这种情况基本无解,不过可以使用其他机器连本机数据库导入导出数据. 今 ...
- 深入探索.NET框架内部了解CLR如何创建运行时对象
原文地址:http://msdn.microsoft.com/en-us/magazine/cc163791.aspx 原文发布日期: 9/19/2005 原文已经被 Microsoft 删除了,收集 ...
- 深入探索.NET内部了解CLR如何创建运行时对象
前言 SystemDomain, SharedDomain, and DefaultDomain. 对象布局和内存细节. 方法表布局. 方法分派(Method dispatching). 因为公共语言 ...
- QTP测试.NET程序的时候,ComboBox下拉框控件选择后,运行时对象不可见解决方案
解决方法: 录制时,选择下拉框数据的时候,不要鼠标单击选择,而是要用ENTER(回车键)来选择,才能完成选择,这样录制就OK了.
- IOS中UIWebView执行javaScript脚本时注意点
1.webView之所以能够滚动,因为它内部有一个UIScrollView子控件 2.移除webView顶部和底部灰色的一层view * 遍历webView中scrollView内部的所有子控件 * ...
- JavaScript是如何工作的:引擎,运行时和调用堆栈的概述!
摘要: 理解JS执行原理. 原文:JavaScript是如何工作的:引擎,运行时和调用堆栈的概述! 作者:前端小智 Fundebug经授权转载,版权归原作者所有. 本文是旨在深入研究JavaScrip ...
随机推荐
- hdu 统计难题(map)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 map的强大之处,但是运行时间太长. 代码: #include <stdio.h> ...
- MVC4的过滤器
过滤器 提供的四种基本类型过滤器接口,IAuthorizationFilter.IActionFilter.IResultFilter和IExceptionFilter,可通过继承对应的接口和Filt ...
- python_基础学习_02_拆分文件(spilt)
做爬虫经常会有这样的引用场景 ,原始网页存储格式为 url+\t+ html php 有个explode的 拆分文本行方法,比较方便直接接收列值 list($url,$html)=explode(& ...
- DYNAMICRESOLUTION | NODYNAMICRESOLUTION
有时候开启OGG进程的时候较慢,可能是由于须要同步的表太多,OGG在开启进程之前会将须要同步的表建立一个记录而且存入到磁盘中,这样就须要耗费大量的时间.OGG同一时候也提供了DYNAMICRESOLU ...
- 九度OJ 1068 球半径和数量 (模拟)
题目1068:球的半径和体积 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4797 解决:1696 题目描写叙述: 输入球的中心点和球上某一点的坐标,计算球的半径和体积 输入: 球的中心 ...
- Android Wear 开发入门——怎样创建一个手机与可穿戴设备关联的通知(Notification)
创建通知 为了创建在手机与可穿戴设备中都能展现的通知,能够使用 NotificationCompat.Builder.通过该类创建的通知,系统会处理该通知是否展如今手机或者穿戴设备中. 导入必要的类库 ...
- MyEclipse使用总结——MyEclipse文件查找技巧
原文:MyEclipse使用总结--MyEclipse文件查找技巧 一.查找文件 使用快捷键[ctrl+shift+R]弹出弹出文件查找框,如下图所示: 二.查找包含某个字符串的文件 使用快捷键[ct ...
- Google Guice结合模式
于Guice于,喷油器装配工作是一个对象图,当请求类型实例,喷油器根据推断对象如何映射到创建的实例.解决依赖.要确定如何解决的依赖就需要配置喷油器结合的方式. 要创建绑定(Binding)对象,能够继 ...
- style中position的属性值具体解释
Position的英文原意是指位置.职位.状态.也有安置的意思.在CSS布局中,Position发挥着非常关键的数据,非常多容器的定位是用Position来完毕. Position属性有四个可选值,它 ...
- Facebook Hack 语言 简介
1. Hack 是什么? Hack 是一种基于HHVM(HipHop VM 是Facebook推出的用来执行PHP代码的虚拟机,它是一个PHP的JIT编译器,同时具有产生快速代码和即时编译的优点.)的 ...