iOS让js调用oc函数

来源:互联网 发布:常用java中间件有哪些 编辑:程序博客网 时间:2024/06/05 22:50

在UIWebView中,代码如下:

  1. //  
  2. //  webview.m  
  3. //  login  
  4. //  
  5. //
  6. // SWWebBrowser.m  Create by William Sterling on 14-1-20.  
  7. //  
  8.   
  9. #import "webview.h"  
  10. #import <JavaScriptCore/JavaScriptCore.h>/*导入库*/ 
  11.   
  12. @implementation webview  
  13.   
  14.   
  15. -(id)initWithFrame:(CGRect)frame  
  16. {  
  17.     self=[super initWithFrame:frame];  
  18.       
  19.     ifself ){  
  20.         self.webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 320self.bounds.size.width300)];  
  21.         self.webview.backgroundColor=[UIColor lightGrayColor];  
  22.         NSString *htmlPath=[[NSBundle mainBundle] resourcePath];  
  23.         htmlPath=[htmlPath stringByAppendingPathComponent:@"html/index.html"];  
  24.         NSURL *localURL=[[NSURL alloc]initFileURLWithPath:htmlPath];  
  25.         [self.webview loadRequest:[NSURLRequest requestWithURL:localURL]];  
  26.         [self addSubview:self.webview];  
  27.   
  28.          JSContext *context = [self.webview valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];  
  29.          context[@"log"] = ^() {  
  30.   
  31.             dlog(@"+++++++Begin Log+++++++");  
  32.             NSArray *args = [JSContext currentArguments];  
  33.   
  34.             for (JSValue *jsVal in args) {  
  35.                  dlog(@"%@", jsVal);  
  36.             }  
  37.   
  38.             JSValue *this = [JSContext currentThis];  
  39.              dlog(@"this: %@",this);  
  40.              dlog(@"-------End Log-------");  
  41.   
  42.         };  
  43.           
  44.   
  45. //        [context evaluateScript:@"log('ider', [7, 21], { hello:'world', js:100 });"];  
  46.   
  47.           
  48.     }  
  49.     return self;  
  50. }  
  51.   
  52.   
  53. @end 

注释:

JSContext *context = [self.webview valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

获取该UIWebview的javascript执行环境。


(2)index.html代码

  1.   
  2. <!DOCTYPE html>  
  3.   
  4. <html lang="en">  
  5.       
  6.     <head>  
  7.           
  8.          <meta charset="utf-8">  
  9.               
  10.           <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">  
  11.             <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />      
  12.                   
  13.             <meta name="description" content="">  
  14.                       
  15.             <meta name="viewport" content="width=device-width; initial-scale=1.0">  
  16.              <script type="text/javascript" src="index.js"></script>             
  17.         
  18.                           
  19.       </head>  
  20.       
  21.     <button id="hallo" onclick="buttonClick()"> 点击button</button>  
  22.   
  23.     </body>  
  24.       
  25. </html> 
  1. function buttonClick()  
  2. {  
  3.     log("hello world");  
  4. }  

意思是点击这个button,就调用jakilllog()函数,jakilllog()函数显然是我们在oc中实现的一个block段,

就是上述绿色部分的block段。

 

点击button会执行么?答案是肯定的。

0 0
原创粉丝点击