UI Base View----UIWebView Summary

Source: Internet
Author: User

UIWebView is a common base view that inherits from UIView in the Uikit framework, and Uilabel,uiimageview is a sibling class for displaying a Web page.

UIWebView is a class that can set up the proxy, which has the corresponding protocol method in different state of loading, and can handle the logic of business level.

Use UIWebView to make a simple mobile browser.

1:webview Basic Properties and methods

The core approach is loadrequest: to load a request, you can implement a Web page.

A more important property is Scalespagetofit scale the page to fit the screen. For websites that do not have a mobile adapter to control whether or not to scale

  Note: When encapsulating a network request, the http://protocol of the URL cannot be omitted.

1- (void) Viewdidload {2 [Super Viewdidload];3     //1: Encapsulating a network request4NSString * URLString =@"Http://www.cnblogs.com/zhangys";//URL is a string5Nsurl * url = [Nsurl urlwithstring:urlstring];//convert URL string to URL6Nsurlrequest * request = [nsurlrequest Requestwithurl:url];//encapsulate the URL as a network request7     //2: Instantiation8UIWebView * WebView = [[UIWebView alloc] initWithFrame:self.view.frame];//instantiate as normal view9     //3: Load RequestTen[WebView Loadrequest:request];//WebView Load Request method, put the encapsulated request in One     //4: Set proxy for current View controller AWebView.Delegate=Self ; -     //5: Zoom the page to fit the screen -Webview.scalespagetofit =YES; the[Self.view Addsubview:webview];//UIView in the hierarchy is important, not on the parent view is not visible -}

2: Proxy method

The current view controller adheres to the Uiwebviewdelegate protocol and sets the current view controller as the proxy for UIWebView, which can implement the method in this protocol, there are four protocol methods, are optional (optional), can be implemented as needed. This implements three, starts loading, ends loading and fails to load.

  Attention:

To use the UIWebView protocol, there must be three parts:

1: Compliance Agreement (Uiwebviewdelegate)

1 @interface Viewcontroller () <UIWebViewDelegate>23@end

2: Set up proxy

1     // 4: Set proxy for current View controller 2     WebView. delegate = self;

3: Implementing Protocol Methods

1 #pragmaMark-uiwebviewdelegate2 3 //called when data is started to load4- (void) Webviewdidstartload: (UIWebView *) WebView5 {6NSLog (@"Start Loading Data");7 }8 //called when loading data is finished9- (void) Webviewdidfinishload: (UIWebView *) WebViewTen { OneNSLog (@"End Load Data"); A } - //called when the load fails -- (void) WebView: (UIWebView *) WebView didfailloadwitherror: (Nserror *) Error the { -NSLog (@"Load Failed"); -}

UI Base View----UIWebView Summary

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.