The easiest way for IOS to use UIWebView to interact with JavaScript

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.