原文地址: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‘);" />
时间: 2024-10-09 12:33:41