UIWebView is the control that renders the mesh in the iOS SDK, and when the page is displayed, we can hack the page and display what we want to display. There is a need to use JavaScript knowledge, and the way UIWebView interacts with JavaScript is stringbyevaluatingjavascriptfromstring:
With this approach we can invoke JavaScript via OBJC and inject JavaScript.
First, let's take a look at how to invoke JavaScript:
[CPP]View Plaincopy
- [WebView stringbyevaluatingjavascriptfromstring:@"myFunction ();"];
[CPP]View Plaincopy
- [WebView stringbyevaluatingjavascriptfromstring:@"myFunction ();"];
Here MyFunction () is our JavaScript method.
To see what to inject into JavaScript, let's start by writing a JavaScript that needs to be injected:
[CPP]View Plaincopy
- function Showalert () {
- Alert (' in Show alert ');
- }
[CPP]View Plaincopy
- function Showalert () {
- Alert (' in Show alert ');
- }
Save As Test.js, and then drag to the resource group under Xcode. Then use the code to inject this JS (as in the Viewdidload method) when initializing.
[CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
- NSString *filepath = [[NSBundle mainbundle] pathforresource:@"test" oftype:@"JS"];
- NSString *jsstring = [[NSString alloc] initwithcontentsoffile:filepath];
- [WebView stringbyevaluatingjavascriptfromstring:jsstring];
[CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
- NSString *filepath = [[NSBundle mainbundle] pathforresource:@"test" oftype:@"JS"];
- NSString *jsstring = [[NSString alloc] initwithcontentsoffile:filepath];
- [WebView stringbyevaluatingjavascriptfromstring:jsstring];
This will inject the above JS, then we can call the JS method at any time, how to invoke, the above is introduced.
Then we can use JS to invoke the OBJC method. Of course, the principle is to use UIWebView Redirect request, pass some commands to our UIWebView, in UIWebView delegate method to receive these commands, and according to the command to execute the corresponding OBJC method. This is equivalent to calling the ObjC method in JavaScript. That's a little bit abstract, just look at the code.
First we write a JavaScript method as follows:
[JavaScript]View Plaincopy < param name= "wmode" value= "Transparent" >
- function SendCommand (cmd,param) {
- var url="TestApp:" +cmd+":" +param;
- document.location = URL;
- }
- function Clicklink () {
- SendCommand ("alert","How are You?") ");
- }
[JavaScript]View Plaincopy < param name= "wmode" value= "Transparent" >
- function SendCommand (cmd,param) {
- var url="TestApp:" +cmd+":" +param;
- document.location = URL;
- }
- function Clicklink () {
- SendCommand ("alert","How are You?") ");
- }
Then call this JS method in your HTML, such as:
[JavaScript]View Plaincopy
- <input type="button" value="click me!" onclick="Clicklink ()" /><br/>
[JavaScript]View Plaincopy
- <input type="button" value="click me!" onclick="Clicklink ()" /><br/>
Finally we intercept this redirect request in 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 *) [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 *) [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;
- }
See the code is not clear enough to understand it? I think PhoneGap may have done so, not studied. But there is an open source project that you can look at, which allows JavaScript to invoke the Objective_c method. Call Jsbridge-to-cocoa.
http://code.google.com/p/jsbridge-to-cocoa/
There are two other related projects
Webviewjavascriptbridge and Gajavascript are worth studying slowly.
"Turn" UIWebView with JavaScript