UIWebView是IOS SDK中渲染網面的控件,在顯示網頁的時候,咱們能夠hack網頁而後顯示想顯示的內容。其中就要用到javascript的知識,而UIWebView與javascript交互的方法就是stringByEvaluatingJavaScriptFromString:javascript
有了這個方法咱們能夠經過objc調用javascript,能夠注入javascript。html
首先咱們來看一下,如何調用javascript:java
- [webView stringByEvaluatingJavaScriptFromString:@"myFunction();"];
- [webView stringByEvaluatingJavaScriptFromString:@"myFunction();"];
這兒myFunction()就是咱們的javascript方法。git
再來看看入何注入javascript,咱們先寫一個須要注入的javascript:github
- function showAlert() {
- alert('in show alert');
- }
- function showAlert() {
- alert('in show alert');
- }
保存爲test.js,而後拖到xcode 的resource分組下。再用代碼在初始化的時候注入這個js(如在viewDidLoad方法裏)。web
- NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"js"];
- NSString *jsString = [[NSString alloc] initWithContentsOfFile:filePath];
- [webView stringByEvaluatingJavaScriptFromString:jsString];
- NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"js"];
- NSString *jsString = [[NSString alloc] initWithContentsOfFile:filePath];
- [webView stringByEvaluatingJavaScriptFromString:jsString];
這樣就注入了上面的js,那麼咱們能夠隨時調用js的方法,如何調用,上面有介紹。xcode
那麼咱們能不能經過js來調用objc的方法呢。 固然能夠,原理就是利用UIWebView重定向請求,傳一些命令到咱們的UIWebView,在UIWebView的delegate的方法中接收這些命令,並根據命令執行相應的objc方法。這樣就至關於在javascript中調用objc的方法。提及來有點抽象,看看代碼一下就明白。app
首先咱們寫一個javascript 方法以下:oop
- function sendCommand(cmd,param){
- var url="testapp:"+cmd+":"+param;
- document.location = url;
- }
- function clickLink(){
- sendCommand("alert","你好嗎?");
- }
- function sendCommand(cmd,param){
- var url="testapp:"+cmd+":"+param;
- document.location = url;
- }
- function clickLink(){
- sendCommand("alert","你好嗎?");
- }
而後在你的html裏調用這個js方法 如:google
- <input type="button" value="Click me!" onclick="clickLink()" /><br/>
- <input type="button" value="Click me!" onclick="clickLink()" /><br/>
最後咱們在UIWebVew中截獲這個重定向請求:
- #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;
- }
- #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 值得你們慢慢研究。