码迷,mamicode.com
首页 > Web开发 > 详细

Objective-c与js相互调用

时间:2015-12-30 00:06:10      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

 

原文地址:http://blog.csdn.net/ikmb/article/details/6716831

一 objective-c调用js

  1. NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];  
  2. //注: webView是UIWebView实例  

 

二 js调用objective-c

1.obj-c部分

  1. - (void)viewDidLoad {  
  2.     [super viewDidLoad];  
  3.     self.myWebView.delegate=self;  
  4. }  
  5. //-------------------------------------------------  
  6. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType   
  7. {  
  8.     //此url解析规则自己定义  
  9.     NSString* rurl=[[request URL] absoluteString];  
  10.     if ([rurl hasPrefix:@"protocol://"]) {  
  11.         UIAlertView* alert=[[UIAlertView alloc]initWithTitle:@"Called by JavaScript"   
  12.                                                      message:@"You‘ve called iPhone provided control from javascript!!" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];  
  13.         [alert show];  
  14.         [alert release];  
  15.         return false;  
  16.     }  
  17.           
  18.     return true;  
  19. }  

2. js部分

  1. function sendCommand(cmd,param){    
  2.     var url="protocol://"+cmd+":"+param;    
  3.     document.location = url;    
  4. }  

3.html部分

  1. <input type="button" value="call obj-c" onClick="sendCommand(‘act‘,‘param‘);" />   

Objective-c与js相互调用

标签:

原文地址:http://www.cnblogs.com/konglei/p/5087279.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!