Objective-c与js相互调用

原文地址: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');" />   

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值