In Uikit, Apple encapsulates a particularly handy control UIWebView enables simple page loading and file loading
First, load the Web Resources 1. Create a new Singleview project, and drag the following control in storyboard
2. Set the related properties connection as follows
3. Class extensions in VIEWCONTROLLER.M comply with Uisearchbardelegate and uiwebviewdelegate
@interface Viewcontroller () <UISearchBarDelegate,UIWebViewDelegate>/** page display */* WebView; @end
4. Extract a method in viewcontroller.m for the browser to load the specified string
//let the browser load the specified string- (void) LoadString: (NSString *) str{NSString*urlstr =str; //1. Determine if the resource path is http://Start if(! [Str hasprefix:@"/ http"]) {//not with http://Start//resources through Baidu searchURLSTR = [NSString stringWithFormat:@"http://m.baidu.com/s?word=%@", str]; } //3. Create a URLNsurl *url =[Nsurl Urlwithstring:urlstr]; //4. Create a requestNsurlrequest *request =[Nsurlrequest Requestwithurl:url]; //5. Load Request[Self.webview loadrequest:request];}
5. Searchbarsearchbuttonclicked: (Uisearchbar *) Searchbar proxy method when the search box enters text and clicks OK (back) button
#pragma mark-Search bar after clicking the Search button call-(void) searchbarsearchbuttonclicked: (Uisearchbar *) searchbar{ //Load specified resource [self LoadString:searchBar.text]; Exit keyboard [Self.view endediting:yes];}
6. If you want to display Baidu page on the homepage, directly in Viewdidload method add Baidu Homepage
-(void) viewdidload{ [Super Viewdidload]; [Self loadString: @" http://m.baidu.com " ]; }
7. Implement the browser's forward and backward function
In the UIWebView control has implemented the forward and backward functions, we only need to and the bottom of the tool bar forward back button for the relevant connection, in the storyboard for the following processing
8. Implement UIWebView proxy method, control whether the forward and backward can be clicked, here do not forget to have these two properties in the class extension
@interface Viewcontroller () <UISearchBarDelegate,UIWebViewDelegate>/** page display */* WebView; /* * Back */ * back; /* * Forward */ *forward; @end
#pragma Mark-uiwebview Proxy Method-(void) Webviewdidfinishload: (UIWebView *) webview{ = Self.webView.canGoBack; = Self.webView.canGoForward;}
Ok,uiwebview Simple loading Web page use, using UIWebView can also load bundle resources, but also easy to learn, here is not elaborated.
iOS Web 3-using UIWebView to do a browser function