This is about iOS! And the method is very simple!! and verify the feasible!!!
1, UIWebView calls the JavaScript function:
nsstring* strvalue = [WebView stringbyevaluatingjavascriptfromstring:@ "Yourfunctionname (' Yourparameter ')"];
The above sentence is called the JavaScript inside the Custom function yourfunctionname (incoming parameter ' yourparameter ', get the return value assigned to strvalue)
2,javascript (via UIWebView) calls the OBJC method:
The following is the simplest and most common way!! After the script is written, it can be dropped to Android:
Is the use of UIWebView (Android is WebView) to intercept the function of the URL!!! Contract a special URL, write in the script, let the upper block to intercept analysis!! Like what
In JavaScript, write:
Window.location.href = "Abcd://xxxyyyzzz"
Inside the iOS:
Implement Uiwebviewdelegate below this function (don't forget to set webview.delegate = self for details)
-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) Navigationtype
{
nsstring* strxxx = Request. Url.resourcespecifier;
Assuming it's just an analysis of "abcd://"
Nsrange range = [strxxx rangeofstring:@ "abcd://"];
if (range.location! = nsnotfound)
{
It is here to perform the operation!!!
Return no;//returns NO, indicating cancellation of navigation to this request
}
return YES;
}
Android, using WebView overload This function can also do the same effect
public boolean shouldoverrideurlloading (WebView view, String URL) {
return true;
}
Thus, when JavaScript executes window.location.href = "abcd://xxxyyyzzz" This sentence, it triggers the upper level of the corresponding operation!
That is, the ability to implement JavaScript calls the upper OBJC function.
The easiest way for IOS to use UIWebView to interact with JavaScript