iOS Web 3-using UIWebView to do a browser function

Source: Internet
Author: User

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

Related Article

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.