From: http://blog.csdn.net/leebing505/article/details/6432326
Purpose:This article focuses on the general local objective-C on iOS systems.CodeThe basic method for interoperability with JavaScript-based Web applications, and the interaction between the two codes is illustrated through examples;
Prerequisites:Familiar with the functions and call methods of the uiwebview in IOS system, the use of the corresponding uiwebviewdelegate delegate, and understand the basic syntax and function call methods of JavaScript functions;
Note:The red text in the body indicates the JavaScript code, and the blue text indicates the OBJECTVIE-C code (there is no way to paste the format, so let's look at it :();
Body:
1.Objective-CCall JavascriptMethod:
Prerequisites:
Uiwebview has been fully loaded to include the page to be called (Note: through monitoring in uiwebviewdelegate hosting-(Void) webviewdidfinishload :( uiwebview *) webviewCall to determine whether the page is loaded ).
Method:
Assume that the page loaded by self. webview, The subview attribute of a view object, contains the following JavaScript Functions:
Function getstring () {return "Hello JavaScript !";}
If this function can be called normally on this page, you can call this function by using the OBJECTVIE-C method shown below:
Nsstring * STR = [self. webview stringbyevaluatingjavascriptfromstring: @ "getstring ();"];
This function actually simulates a JavaScript function call on the page. Therefore, any valid JavaScript code within the function can be executed! The return value of this objective-C code is the return value of the called JavaScript code. In the preceding example, the STR value is assigned as @ "Hello JavaScript !".
The called function can contain string or numeric parameters. If the called JavaScript function name contains parameters, ensure that the format of the passed parameters is correct. In particular, the quotation marks of string parameters are easily ignored.
2.System response page JavascriptCode:
Prerequisites:
To implement uiwebviewdelegate
-(Bool) webview :( uiwebview *) webview
Shouldstartloadwithrequest :( nsurlrequest *) Request
Navigationtype :( uiwebviewnavigationtype) navigationtype;
Managed functions (hereinafter referred to as jump monitoring functions) and set the objects implementing the managed functions to the delegate of uiwebview.
Method:
When the current page of uiwebview is redirected by JavaScript code in the following way, the jump monitoring function is called:
Window. Location. href = "http://www.strongsoft.net ";
In this case, the jump monitoring function of the object implementing the managed protocol will be called, and the browser jump address will be monitored using the following code:
Nsstring * url = [[request URL] absolutestring];
If the value of the managed function is no, the page jump of the page uiwebview is denied. With this idea, you can monitor jump addresses in specific formats on the page to intercept and execute the corresponding local code to implement interaction between JavaScript and objective-C code.
For example, it is required to intercept all URLs with the prefix "objc:" and use if... Else... To determine the code to be executed locally, the implementation is shown in the following format:
-(Bool) webview :( uiwebview *) webview
Shouldstartloadwithrequest :( nsurlrequest *) Request
Navigationtype :( uiwebviewnavigationtype) navigationtype
{
Nsstring * urlstring = [[request URL] absolutestring];
Nsarray * urlcomps = [urlstring componentsseparatedbystring: @ ":"];
If ([urlcomps count] & [[urlcomps objectatindex: O] isequaltostring: @ "objc"])
{
Nsstring * funcstr = [urlcomps objectatindex: 1];
If ([funcstr isequaltostring: @ "dofunc1"])
{
/* Call the local function 1 */
}
Else if ([funcstr isequaltostring: @ "dofunc2"])
{
/* Call the local function 2 */
}
Return no;
}
Return yes;
}
When you need to call the local function 1, you can use the following JavaScript function:
Window. Location. href = "objc: dofunc1 ";
Based on the above ideas, we can use JavaScript to call the objective-C code with Local parameters.