UIWebView是IOS SDK中渲染网面的控件,在显示网页的时候,我们可以hack网页然后显示想显示的内容。其中就要用到javascript的知识,而UIWebView与javascript交互的方法就是stringByEvaluatingJavaScriptFromString:
有了这个方法我们可以通过objc调用javascript,可以注入javascript。
首先我们来看一下,如何调用javascript:
[cpp] view plaincopy
- [webView stringByEvaluatingJavaScriptFromString:@"myFunction();"];
[cpp] view plaincopy
- [webView stringByEvaluatingJavaScriptFromString:@"myFunction();"];
这儿myFunction()就是我们的javascript方法。
再来看看入何注入javascript,我们先写一个需要注入的javascript:
[cpp] view plaincopy
- function showAlert() {
- alert(‘in show alert‘);
- }
[cpp] view plaincopy
- function showAlert() {
- alert(‘in show alert‘);
- }
保存为test.js,然后拖到xcode 的resource分组下。再用代码在初始化的时候注入这个js(如在viewDidLoad方法里)。
[cpp] view plaincopy
- NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"js"];
- NSString *jsString = [[NSString alloc] initWithContentsOfFile:filePath];
- [webView stringByEvaluatingJavaScriptFromString:jsString];
[cpp] view plaincopy
- NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"js"];
- NSString *jsString = [[NSString alloc] initWithContentsOfFile:filePath];
- [webView stringByEvaluatingJavaScriptFromString:jsString];
这样就注入了上面的js,那么我们可以随时调用js的方法,如何调用,上面有介绍。
那么我们能不能通过js来调用objc的方法呢。 当然可以,原理就是利用UIWebView重定向请求,传一些命令到我们的UIWebView,在UIWebView的delegate的方法中接收这些命令,并根据命令执行相应的objc方法。这样就相当于在javascript中调用objc的方法。说起来有点抽象,看看代码一下就明白。
首先我们写一个javascript 方法如下:
[javascript] view plaincopy
- function sendCommand(cmd,param){
- var url="testapp:"+cmd+":"+param;
- document.location = url;
- }
- function clickLink(){
- sendCommand("alert","你好吗?");
- }
[javascript] view plaincopy
- function sendCommand(cmd,param){
- var url="testapp:"+cmd+":"+param;
- document.location = url;
- }
- function clickLink(){
- sendCommand("alert","你好吗?");
- }
然后在你的html里调用这个js方法 如:
[javascript] view plaincopy
- <input type="button" value="Click me!" onclick="clickLink()" /><br/>
[javascript] view plaincopy
- <input type="button" value="Click me!" onclick="clickLink()" /><br/>
最后我们在UIWebVew中截获这个重定向请求:
[cpp] view plaincopy
- #pragma mark --
- #pragma mark UIWebViewDelegate
- - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
- NSString *requestString = [[request URL] absoluteString];
- NSArray *components = [requestString componentsSeparatedByString:@":"];
- if ([components count] > 1 && [(NSString *)[components objectAtIndex:0] isEqualToString:@"testapp"]) {
- if([(NSString *)[components objectAtIndex:1] isEqualToString:@"alert"])
- {
- UIAlertView *alert = [[UIAlertView alloc]
- initWithTitle:@"Alert from Cocoa Touch" message:[components objectAtIndex:2]
- delegate:self cancelButtonTitle:nil
- otherButtonTitles:@"OK", nil];
- [alert show];
- }
- return NO;
- }
- return YES;
- }
[cpp] view plaincopy
- #pragma mark --
- #pragma mark UIWebViewDelegate
- - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
- NSString *requestString = [[request URL] absoluteString];
- NSArray *components = [requestString componentsSeparatedByString:@":"];
- if ([components count] > 1 && [(NSString *)[components objectAtIndex:0] isEqualToString:@"testapp"]) {
- if([(NSString *)[components objectAtIndex:1] isEqualToString:@"alert"])
- {
- UIAlertView *alert = [[UIAlertView alloc]
- initWithTitle:@"Alert from Cocoa Touch" message:[components objectAtIndex:2]
- delegate:self cancelButtonTitle:nil
- otherButtonTitles:@"OK", nil];
- [alert show];
- }
- return NO;
- }
- return YES;
- }
看了代码是不是清楚得不能再清楚了呀? 我想phonegap可能与是这样实现的,没去研究过。 不过有一个开源工程大家可以看看,它允许javascript调用objective_c的方法。叫jsbridge-to-cocoa
http://code.google.com/p/jsbridge-to-cocoa/
还有两个相关工程
WebViewJavascriptBridge 与 GAJavaScript 值得大家慢慢研究。