WebView 和Native交互
Method parameters must be objects or
basic data types like int and float. Objective-C objects will be
converted to their JavaScript equivalents by WebKit:
NSNumber objects will be converted to JavaScript numbers.
NSString objects will be converted to JavaScript strings.
NSArray objects will be mapped to special read-only arrays.
NSNull will be converted to JavaScript’s null.
All other objects will be wrapped by WebKit into appropriate objects for the JavaScript environment.
1 call JS的方法
Js中有方法:function addImage(image, width, height) { … }
Native有两种方式call
1.1
id win = [webView windowScriptObject];
NSArray *args = [NSArray arrayWithObjects:
@”sample_graphic.jpg”,
[NSNumber numberWithInt:320],
[NSNumber numberWithInt:240],
nil];
[win callWebScriptMethod:@"addImage"
withArguments:args];
1.2
[win evaluateWebScript:
@"addImage(’sample_graphic.jpg’, ‘320’, ‘240’)”];
参考:https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DisplayWebContent/Tasks/JavaScriptFromObjC.html
2 js回调native的方法
2.1 Structures and non object pointers will not be passed to JavaScript.
参考:
https://developer.apple.com/library/content/documentation/AppleApplications/Conceptual/SafariJSProgTopics/ObjCFromJavaScript.html#//apple_ref/doc/uid/30001215-BBCBFJCD
https://developer.apple.com/reference/webkit?language=objc
http://www.cnblogs.com/yueyuanyueyuan/p/5651266.html
JavaScriptCore源码地址
https://opensource.apple.com/source/JavaScriptCore/
https://chromium.googlesource.com/external/WebKit_trimmed/+/HEAD/Source/JavaScriptCore/API
—Open Source:
macgap
参考文章:
http://www.cnblogs.com/yueyuanyueyuan/p/5651266.html
http://www.jianshu.com/p/939db6215436
http://www.jianshu.com/p/0042d8eb67c0