App and WebView interaction

Source: Internet
Author: User

We often use the UIWebView in the development, when the code in the embedded H5, our app may need and webview, such as the Web front-end engineer to write a JS in a sharing function, when he clicked on a share button in H5, We need to invoke the sharing feature inside our app. such as pop-up sharing channel interface, to share. At this point, you can discuss with the front-end engineer to load a custom URL. For example: native://share?title=xx&link=xxx. We get the required parameter content by parsing this URL. Uiwebviewdelegate in the following way

-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) navigationtype{    nsurl *url = Request. URL;    NSLog (@ "WebView url:%@", url);    if ([[URL scheme] isequaltostring:@ "native"])    {        return [self handlenativeurl:url];            }        if (![ _webview CanGoBack])    {        if (self.isloadmessages)        {            self.navigationitem.leftbarbuttonitem=nil;        }    }                    return YES;}

Gets the URL to load to achieve webview interaction with the app.


UIWebView also has a strong point is that you can call the JS function directly.

-(NSString *) stringbyevaluatingjavascriptfromstring: (NSString *) script;
This method can directly invoke the JS method and return the result of the call. For example, get to the title of the page.

-(void) Webviewdidfinishload: (UIWebView *) webview{  //  NSLog (@ "title:%@", [WebView stringbyevaluatingjavascriptfromstring:@ "Document.title"]);    if (self.isloadmessages)//Only when the information is loaded    {        if ([WebView cangoback])//Can be returned        {            [self setnavigationbar] ;//Add fake navigation entry        }                self.navigationitem.title=[webview stringbyevaluatingjavascriptfromstring:@ " Document.title "]; Call JS, get title    }}



Interaction between apps and WebView

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.