Uiwebview webpage view-iOS development

Source: Internet
Author: User

Uiwebview allows you to create a web browser, similar to Safari, instead of starting safsri in the program. Do you think it's great? Let's talk nonsense and start with the topic.

1. Create a uiwebview

CGRect bouds = [[UIScreen manScreen]applicationFrame];UIWebView* webView = [[UIWebView alloc]initWithFrame:bounds];

Ii. Set attributes

Webview. scalespagetofit = yes; // automatically scales the page to adapt to webview. detectsphonenumbers = yes; // automatically detects the phone number on the page, and click

3. Display webpage view uiwebview

[self.view addSubview:webView];

4. Load content

Nsurl * url = [nsurl urlwithstring: @ "http://www.youku.com"]; // create urlnsurlrequest * request = [nsurlrequest requestwithurl: url]; // create nsurlrequest [webview loadrequest: request]; // Load

You can also load a local resource:

Nsurl * url = [nsurl fileurlwithpath: filepath]; // create urlnsurlrequest * request = [nsurlrequest requestwithurl: url]; // create nsurlrequest [webview loadrequest: request]; // Load

Uiwebview also supports loading an nsstring object as the source. You can provide a basic URL to guide the uiwebview object to follow the link and load remote resources:

[webView loadHTMLString:myHTML baseURL:[NSURL URLWithString:@"http://baidu.com"]];

5. Navigation

The uiwebview class manages browser navigation actions. You can use the goforward and Goback methods to control the forward and backward actions:

[Webview Goback]; [webview goforward]; [webview reload]; // overload [webview stoploading]; // cancel loading content

Vi. uiwebviewdelegate delegate proxy

Uiwebview supports a set of delegate methods, which are notified at a specific time. To use these methods, you must first set the webview delegate:

webView.delegate = self;

The first parameter of each delegate method below is a pointer to a uiwebview. Therefore, you can use a delegate to multiple webpage views.

-(Bool) webview :( uiwebview *) webview shouldstartloadwithrequest :( nsurlrequest *) reuqest navigationtype :( uiwebviewnavigationtype) navigationtype; // when the webpage view is instructed to load the content, it is notified. Yes should be returned for loading. You can obtain the reason for the request initiation through the navigation type parameter, which can be any of the following values: Repeated reloaduiwebviewnavigationtypeformresubmitteduiwebviewnavigationtypeother
-(Void) webviewdidstartload :( uiwebview *) webview; // you will be notified when the webpage view has started to load a request.
-(Void) webviewdidfinishload :( uiwebview *) webview; // you will be notified after the webpage view ends loading a request.
-(Void) webview :( uiwebview *) webview didfailloadwitherror :( nserror *) error; // get a notification when an error occurs during request loading. An nsserror object is provided to identify the error type.


So far, I have finished all the work. Do you think it is very useful? Are you trying to write a demo?

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.