Click on UIWebView text to invoke method

Source: Internet
Author: User

Sometimes it is necessary to call a method or event by clicking on the text or button in the Web page , and we need to make a connection between the Web page and the app.

1. contract with server personnel for a protocol such as ios://

2. When you click on the text or button on the page, jump to the path of this Convention protocol

3. Use the webview proxy method to listen to the URL and determine which operation determines the calling method.

window.location.href = ' Iso://opencamer ';


#pragma mark-uiwebviewdelegate/** This method is called before WebView sends a request, returns Yes, can load the request, returns no, and disables loading of this request */-(BOOL) WebView: ( UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: (Uiwebviewnavigationtype) navigationtype{    NSString *url = Request. url.absolutestring;    Nsrange range = [url rangeofstring:@ "ios://"];    if (range.length! = 0) {        //Intercept method name        nsstring *method = [url substringFromIndex:range.location + range.length];        Convert the method name to SEL type        sel selector = nsselectorfromstring (method);        [Self performselector:selector withobject:nil];    }    return YES;}



Click on UIWebView text to invoke method

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.