A. Basic use1. Concept iOS built-in browser controls the Safari browser is implemented via UIWebView 2. Use: Make a simple browser (1) Basic request Create Request load request (2) agent listens for WebView load, for example prompt is loading (3) forward, Back to use Navigationcontroller, add the navigation bar at the bottom (tick showtoolbar), plus the fallback and Forward buttons (4) to the top navigation bar plus the Address bar Uisearchbar agent Listening "search" button, extract the Address bar text
1 //2 //VIEWCONTROLLER.M3 //Uiwebviewddemo4 //5 //Created by Hellovoidworld on 15/1/30.6 //Copyright (c) 2015 Hellovoidworld. All rights reserved.7 //8 9 #import "ViewController.h"Ten One @interfaceViewcontroller () <uisearchbardelegate, uiwebviewdelegate> A -@property (Weak, nonatomic) iboutlet UIWebView *WebView; - the@property (Weak, nonatomic) Iboutlet Uibarbuttonitem *Backbutton; -@property (Weak, nonatomic) Iboutlet Uibarbuttonitem *Forwardbutton; --(Ibaction) Back: (Uibarbuttonitem *) sender; --(Ibaction) Forward: (Uibarbuttonitem *) sender; + - @end + A @implementationViewcontroller at -- (void) Viewdidload { - [Super Viewdidload]; - //additional setup after loading the view, typically from a nib. - - //Create a search bar inUisearchbar *searchbar =[[Uisearchbar alloc] init]; -Searchbar.frame = CGRectMake (0,0, -, +); toSearchbar.Delegate=Self ; + -Self.navigationItem.titleView =Searchbar; the *Self.webview.Delegate=Self ; $ }Panax Notoginseng - the +-(Ibaction) Back: (Uibarbuttonitem *) Sender { ANSLog (@"back"); the [Self.webview GoBack]; + } - $-(Ibaction) Forward: (Uibarbuttonitem *) Sender { $NSLog (@"forward"); - [Self.webview GoForward]; - } the - #pragmaMark-uiwebviewdelegateWuyi- (void) Webviewdidstartload: (UIWebView *) WebView { theNSLog (@"Start loading"); - } Wu -- (void) Webviewdidfinishload: (UIWebView *) WebView { AboutNSLog (@"Finish Loading"); $ - //set back, Forward button - if([Self.webview CanGoBack]) { -self.backButton.enabled =YES; A}Else { +self.backButton.enabled =NO; the } - $ if([Self.webview CanGoForward]) { theself.forwardButton.enabled =YES; the}Else { theself.forwardButton.enabled =NO; the } - } in the #pragmaMark-uisearchbardelegate the /** Click the Search button (on the virtual keyboard)*/ About- (void) searchbarsearchbuttonclicked: (Uisearchbar *) Searchbar { theNSString *str =Searchbar.text; theNsurl *url = [Nsurl urlwithstring:[nsstring stringWithFormat:@"http://%@", str]]; theNsurlrequest *request =[Nsurlrequest Requestwithurl:url]; + - [Self.webview loadrequest:request]; the }Bayi the @end3. Load local resources HTML, TXT, doc, ppt, MP4, PDF direct Request can be 4. Automatically identify phone, mailbox, address//Identify all types of data Self.webView.dataDetectorTypes = Uidatadetectorty Peall;
B. Other uses1.OC executing javascript
1 /* */2 - (ibaction) runjs {3 @ "alert (' Hello, world! '); " ; 4 [Self.webview stringbyevaluatingjavascriptfromstring:str]; 5 }
2.javascript call OC in JavaScript to send Web requests, using OC intercept, according to the content of the decision to call the appropriate method here to use the JS Code outbound album selection: web & JS Code:
1 <%@ Page Language="Java"ContentType="text/html; Charset=utf-8"2 pageencoding="UTF-8"%>3 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd ">4 <HTML>5 <Head>6 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">7 <title>Run OC Code</title>8 </Head>9 <Body>Ten One <Scripttype= "Text/javascript"> A functionRunoc () { - window.location.href="Ios://openalbum"; - } the </Script> - - <inputtype= "button"value= "Run OC Code"onclick= "Runoc ();" /> - + </Body> - </HTML>OC Code:
1 /** WebView will be called before sending the request, can intercept the request, choose to load (return YES) or not (return no)*/2-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (Nsurlrequest *Request Navigationtype: (Uiwebviewnavigationtype) Navigationtype {3 4 //Intercept Request5NSString *urlstr =request. url.absolutestring;6Nsrange range = [Urlstr rangeofstring:@"ios://"];7 8 //if it is an iOS request9 if(Range.length! =0) {Ten //Method Name OneNSString *method = [Urlstr substringFromIndex:range.location +Range.length]; A - //Package sel -SEL selector =nsselectorfromstring (method); the - //Execution Method - [self performselector:selector withobject:nil]; - } + - returnYES; + } A at /** Open Album*/ -- (void) Openalbum { - //Create a photo selection controller -Uiimagepickercontroller *IMAGEPC =[[Uiimagepickercontroller alloc] init]; -[Imagepc Setsourcetype:uiimagepickercontrollersourcetypephotolibrary];//use album Source - in[Self Presentviewcontroller:imagepc animated:yes completion:^{ -NSLog (@"Call out Albums"); to }]; +}
[iOS base Control-7.0] UIWebView