Uiwebview is a control used to render the network surface in ios sdk. When displaying a webpage, we can hack the webpage and then display the desired content. The javascript knowledge is required, and the interaction between uiwebview and JavaScript is stringbyevaluatingjavascriptfromstring:
With this method, we can call JavaScript through objc and inject Javascript.
First, let's take a look at how to call javascript:
[webView stringByEvaluatingJavaScriptFromString:@"myFunction();"];
Here myfunction () is our JavaScript method.
Let's take a look at how Javascript is injected. Let's first write a JavaScript to be injected:
function showAlert() {alert('in show alert');}
Save as test. js and drag it to the Resource Group of xcode. Use the code to inject this JS (such as in the viewdidload method) during initialization ).
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"js"];NSString *jsString = [[NSString alloc] initWithContentsOfFile:filePath];[webView stringByEvaluatingJavaScriptFromString:jsString];
In this way, the above JS is injected, so we can call the js method at any time and how to call it, which is described above.
So can we call the objc method through JS. Of course, the principle is to use the uiwebview redirection request to upload some commands to our uiwebview, receive these commands in the delegate method of uiwebview, and execute the corresponding objc method according to the command. This is equivalent to calling the objc method in JavaScript. It is a little abstract. Let's take a look at the code to understand it.
First, write a JavaScript method as follows:
Function sendcommand (CMD, Param) {var url = "testapp:" + cmd + ":" + Param; document. location = URL;} function clicklink () {sendcommand ("alert", "How are you? ");}
Then, you can call this js method in your HTML:
<input type="button" value="Click me!" onclick="clickLink()" /><br/>
Finally, we intercepted the redirection request in 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;}
Can I see the code clearly? I think phonegap may be implemented in this way. I haven't studied it yet. But there is an open-source project that you can see. It allows JavaScript to call the objective_c method. Jsbridge-to-Cocoa
Http://code.google.com/p/jsbridge-to-cocoa/
There are two other related projects
Webviewjavascriptbridge and gajavascript are worth your research.