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 }}