Interaction between apps and WebView

Source: Internet
Author: User

Interaction between apps and WebView

UIWebView is often used in development. When h5 is embedded in the code, our app may need to communicate with WebView, for example, a sharing Function in js written by web Front-end engineers, when he clicks a share button in h5, we need to call the share function in our app. For example, the sharing channel interface is displayed for sharing. At this time, you can discuss with the front-end engineer to load a custom url. For example: native: // share? Title = xx & link = xxx. We can parse this url to obtain the required parameter content. In UIWebViewDelegate, use the following method:

-(BOOL) webView :( UIWebView *) webView shouldStartLoadWithRequest :( NSURLRequest *) request navigationType :( UIWebViewNavigationType) navigationType {NSURL * url = request. URL; NSLog (@ "webView url: % @", url); if ([[url scheme] isw.tostring: @ "native"]) {return [self handleNativeUrl: url];} if (! [_ Webview canGoBack]) {if (self. isLoadMessages) {self. navigationItem. leftBarButtonItem = nil ;}} return YES ;}

Obtain the loaded url to achieve webView interaction with the app.


Another strength of UIWebView is that JavaScript functions can be called directly.

- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;
This method can directly call the js method and return the call result. For example, you can obtain the title of a webpage.

-(Void) webViewDidFinishLoad :( UIWebView *) webView {// NSLog (@ "title: % @", [webView stringByEvaluatingJavaScriptFromString: @ "document. title "]); if (self. isLoadMessages) // only {if ([webView canGoBack]) when loading information // {[self setNavigationBar] When return can be added; // Add false navigation entries} self. navigationItem. title = [webView stringByEvaluatingJavaScriptFromString: @ "document. title "]; // call js to obtain the title }}



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.