iOS让js调用oc函数
来源:互联网 发布:常用java中间件有哪些 编辑:程序博客网 时间:2024/06/05 22:50
在UIWebView中,代码如下:
- //
- // webview.m
- // login
- //
- //
- // SWWebBrowser.m Create by William Sterling on 14-1-20.
- //
- #import "webview.h"
- #import <JavaScriptCore/JavaScriptCore.h>/*导入库*/
- @implementation webview
- -(id)initWithFrame:(CGRect)frame
- {
- self=[super initWithFrame:frame];
- if( self ){
- self.webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 320, self.bounds.size.width, 300)];
- self.webview.backgroundColor=[UIColor lightGrayColor];
- NSString *htmlPath=[[NSBundle mainBundle] resourcePath];
- htmlPath=[htmlPath stringByAppendingPathComponent:@"html/index.html"];
- NSURL *localURL=[[NSURL alloc]initFileURLWithPath:htmlPath];
- [self.webview loadRequest:[NSURLRequest requestWithURL:localURL]];
- [self addSubview:self.webview];
- JSContext *context = [self.webview valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
- context[@"log"] = ^() {
- dlog(@"+++++++Begin Log+++++++");
- NSArray *args = [JSContext currentArguments];
- for (JSValue *jsVal in args) {
- dlog(@"%@", jsVal);
- }
- JSValue *this = [JSContext currentThis];
- dlog(@"this: %@",this);
- dlog(@"-------End Log-------");
- };
- // [context evaluateScript:@"log('ider', [7, 21], { hello:'world', js:100 });"];
- }
- return self;
- }
- @end
注释:
JSContext *context = [self.webview valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
获取该UIWebview的javascript执行环境。
(2)index.html代码
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta name="description" content="">
- <meta name="viewport" content="width=device-width; initial-scale=1.0">
- <script type="text/javascript" src="index.js"></script>
- </head>
- <button id="hallo" onclick="buttonClick()"> 点击button</button>
- </body>
- </html>
- function buttonClick()
- {
- log("hello world");
- }
意思是点击这个button,就调用jakilllog()函数,jakilllog()函数显然是我们在oc中实现的一个block段,
就是上述绿色部分的block段。
点击button会执行么?答案是肯定的。
0 0
- iOS让js调用oc函数
- iOS-JS调用OC
- JS调用OC函数
- ios--js调用oc(实例)
- iOS js调用oc方法
- iOS js oc相互调用(JavaScriptCore)
- iOS js oc相互调用(JavaScriptCore)
- iOS js oc相互调用(JavaScriptCore)
- iOS js oc相互调用(JavaScriptCore)
- iOS js oc相互调用(JavaScriptCore)
- iOS js oc相互调用(JavaScriptCore)
- IOS中OC与JS互相调用
- iOS js oc相互调用(JavaScriptCore)
- iOS js oc相互调用(JavaScriptCore)
- iOS js oc相互调用(JavaScriptCore)
- iOS-OC调用JS的多种形式
- iOS js oc相互调用(JavaScriptCore)
- iOS js oc相互调用(JavaScriptCore)
- VMware虚拟机上网络连接类型(Bridged、Host-only、NAT)的三种模式
- Storm DRPC 介绍
- leetcode Word Search
- SQL注入
- UE 调出 搜索结果列表
- iOS让js调用oc函数
- Remove Duplicates from Sorted List II
- Reverse Linked List
- 2015进入博客的世界
- Speex 同时适用于 Ios 与 Android 【代码篇 一】
- php中错误和异常处理的机制一
- codevs 1069关押罪犯 并查集
- 怎样查询SQL数据库中某一个表中的某个列的一个数值的所有行数据
- order---linux下查找大于某数值的文件