原文地址:http://blog.csdn.net/ikmb/article/details/6716831
一 objective-c调用js
- NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
- //注: webView是UIWebView实例
二 js调用objective-c
1.obj-c部分
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.myWebView.delegate=self;
- }
- //-------------------------------------------------
- - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
- {
- //此url解析规则自己定义
- NSString* rurl=[[request URL] absoluteString];
- if ([rurl hasPrefix:@"protocol://"]) {
- UIAlertView* alert=[[UIAlertView alloc]initWithTitle:@"Called by JavaScript"
- message:@"You've called iPhone provided control from javascript!!" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
- [alert show];
- [alert release];
- return false;
- }
- return true;
- }
2. js部分
- function sendCommand(cmd,param){
- var url="protocol://"+cmd+":"+param;
- document.location = url;
- }
3.html部分
<input type="button" value="call obj-c" onClick="sendCommand('act','param');" />
本文介绍了Objective-C与JavaScript之间的相互调用方法。Objective-C可以通过UIWebView实例调用JavaScript代码获取页面URL。反之,JavaScript可以通过定义特定URL格式来触发Objective-C中的事件。
425

被折叠的 条评论
为什么被折叠?



