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