此解決方案原理:
1、在ViewController.h中聲明方法和成員變量,以及webView的委托:
// // ViewController.h // JS_IOS_01 // // Created by IMAC on 14-2-24. // Copyright (c) 2014年 Wanggsx. All rights reserved. // #import@interface ViewController : UIViewController {} @property (nonatomic,retain) IBOutlet UIWebView *webView; // 兩個參數 -(void)getParam1:(NSString*)str1 withParam2:(NSString*)str2; @end
2、在ViewController.m中合成成員變量並實現該方法:
// // ViewController.m // JS_IOS_01 // // Created by IMAC on 14-2-24. // Copyright (c) 2014年 Wanggsx. All rights reserved. // #import ViewController.h @interface ViewController () @end @implementation ViewController @synthesize webView; - (void)viewDidLoad { [super viewDidLoad]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)getParam1:(NSString*)str1 withParam2:(NSString*)str2 { NSLog(@收到html傳過來的參數:str1=%@,str2=%@,str1,str2); } @end
- (void)viewDidLoad { [super viewDidLoad]; webView.backgroundColor = [UIColor clearColor]; //webView.scalesPageToFit =YES; webView.delegate =self; NSString *basePath = [[NSBundle mainBundle]bundlePath]; NSString *helpHtmlPath = [basePath stringByAppendingPathComponent:@jsIOS.html]; NSURL *url = [NSURL fileURLWithPath:helpHtmlPath]; NSURLRequest *request=[NSURLRequest requestWithURL:url]; [webView loadRequest:request]; }
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType { NSString *urlString = [[request URL] absoluteString]; urlString = [urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSLog(@urlString=%@,urlString); NSArray *urlComps = [urlString componentsSeparatedByString:@://]; if([urlComps count] && [[urlComps objectAtIndex:0] isEqualToString:@objc]) { NSArray *arrFucnameAndParameter = [(NSString*)[urlComps objectAtIndex:1] componentsSeparatedByString:@:/]; NSString *funcStr = [arrFucnameAndParameter objectAtIndex:0]; if (1 == [arrFucnameAndParameter count]) { // 沒有參數 if([funcStr isEqualToString:@doFunc1]) { /*調用本地函數1*/ NSLog(@doFunc1); } } else { //有參數的 if([funcStr isEqualToString:@getParam1:withParam2:]) { [self getParam1:[arrFucnameAndParameter objectAtIndex:1] withParam2:[arrFucnameAndParameter objectAtIndex:2]]; } } return NO; } return TRUE; }
以下是完整的ViewController.m的代碼:
// // ViewController.m // JS_IOS_01 // // Created by IMAC on 14-2-24. // Copyright (c) 2014年 Wanggsx. All rights reserved. // #import ViewController.h @interface ViewController () @end @implementation ViewController @synthesize webView; - (void)viewDidLoad { [super viewDidLoad]; webView.backgroundColor = [UIColor clearColor]; //webView.scalesPageToFit =YES; webView.delegate =self; NSString *basePath = [[NSBundle mainBundle]bundlePath]; NSString *helpHtmlPath = [basePath stringByAppendingPathComponent:@jsIOS.html]; NSURL *url = [NSURL fileURLWithPath:helpHtmlPath]; NSURLRequest *request=[NSURLRequest requestWithURL:url]; [webView loadRequest:request]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType { NSString *urlString = [[request URL] absoluteString]; urlString = [urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSLog(@urlString=%@,urlString); NSArray *urlComps = [urlString componentsSeparatedByString:@://]; if([urlComps count] && [[urlComps objectAtIndex:0] isEqualToString:@objc]) { NSArray *arrFucnameAndParameter = [(NSString*)[urlComps objectAtIndex:1] componentsSeparatedByString:@:/]; NSString *funcStr = [arrFucnameAndParameter objectAtIndex:0]; if (1 == [arrFucnameAndParameter count]) { // 沒有參數 if([funcStr isEqualToString:@doFunc1]) { /*調用本地函數1*/ NSLog(@doFunc1); } } else { //有參數的 if([funcStr isEqualToString:@getParam1:withParam2:]) { [self getParam1:[arrFucnameAndParameter objectAtIndex:1] withParam2:[arrFucnameAndParameter objectAtIndex:2]]; } } return NO; } return TRUE; } -(void)getParam1:(NSString*)str1 withParam2:(NSString*)str2 { NSLog(@收到html傳過來的參數:str1=%@,str2=%@,str1,str2); } @end