Getting Started with iOS development-WebView

Source: Internet
Author: User
Tags call back

Transferred from: http://mahaile.blog.51cto.com/2891586/1021515/

Target: WebView use

In some mobile apps, sometimes the program will be loaded into the appearance of the HTML interface, Android has WebView iOS have it, the answer is yes, below we look at the iOS under the WebView is how to use it

Launch Xcode to create a new iOS project, template Select Signle View application, project name named Iphone_sample_webview class Prefix named: WebView

To enter the project, open the Webviewviewcontroller.xib view file and drag a WebView component into the view

Click Assistant Editor

Now the interface is as follows

To establish a connection for WebView: Hold down the CTRL key and drag the WebView component into the WebViewViewController.h file @interface webviewviewcontroller:uiviewcontroller&l T Below the uiwebviewdelegate> code:

Release the mouse, the position is correct, such as, will pop up a dialog box, let you fill in the connection information: we only fill in the name value can be filled in the name content box "WebView" Click Connent Complete the connection

By doing this, Xcode generates a series of code for us, generates a output in the WebViewViewController.h file, and generates a management method for the WebView object in the Webviewviewcontroller.m file. You'll know when you watch your kids ' shoes carefully.

Below we add behavior for WebView in the WEBVIEWVIEWCONTROLLER.M controller

Add code in the-(void) Viewdidload method of the Webviewviewcontroller.m file

-(void) viewdidload
{
[Super Viewdidload];
Nsurlrequest *request=[nsurlrequest requestwithurl:[nsurl urlwithstring:@ "http://www.baidu.com"];
[Self.webview Loadrequest:request];
Additional setup after loading the view, typically from a nib.
}

Run our simulator to see, then we can already open the Baidu address in the view. :

But this is not enough in practical application, because the problem of mobile network, may appear the HTML page can not open, open slowly, or error, and so on, we need to sit the corresponding prompt operation. So we also need to continue to give webview optimization, so that the user experience better.

Let's take a look at some common methods of WebView

UIWebView there are usually several ways to load a

1.loadRequest:

2.loadhtmlstring:string BaseURL:

3.loaddata:mimetype:textencodingname:baseurl:

Where BaseURL refers to the base of the URL is an absolute address, the program to use the other resources can be based on this reference address to find without the need to locate the absolute address;

Two. Several important functions in the UIWebView

1.-(void) Webviewdidstartload: (UIWebView *) Called when the page starts to load WebView

2.-(void) Webviewdidfinishload: (UIWebView *) WebView when page loading is complete

3.-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) Navigationtype

This function is called when the program is loaded with UIWebView load mode 1, and then the Webviewdidstartload function is executed, so we can do some request parsing and URL parsing work in this function.

4.-(void) WebView: (UIWebView *)webView didfailloadwitherror: (nserror *)error

is an optional function, if the page load failure can be based on different error type feedback to the user different information

So how do we implement these methods in the code?

1: In the WebViewViewController.h file, declare the protocol in. h to add the following red section of code

@interface webviewviewcontroller:uiviewcontroller<uiwebviewdelegate>
@property (Retain, nonatomic) Iboutlet UIWebView *webview;

@end

2: Implement the following methods in the WEBVIEWVIEWCONTROLLER.M file

1.-(void) Webviewdidstartload: (UIWebView *) Called when the page starts to load WebView
2.-(void) Webviewdidfinishload: (UIWebView *) WebView when page loading is complete
3.-(void) WebView: (UIWebView *) WebView didfailloadwitherror: (nserror *) error when the page is loaded incorrectly

We can do this in a couple of ways.

Then continue above, sit down, at the time of loading to the user waiting for prompt, loading completed after the cancellation of waiting prompt

1: Open the Webviewviewcontroller.xib file in the view file, drag a actionvity Indicationview component, position it, and add connection to this step and WebView add connection step is the same as forget to look up the top. Hey

2: Add a delegate to the WebView in the webviewviewcontroller.m file-(void) Viewdidload method to add a delegate to WebView eventually as in the following code

-(void) viewdidload
{
[Super Viewdidload];
[Self.webview setdelegate:self];
Nsurlrequest *request=[nsurlrequest requestwithurl:[nsurl urlwithstring:@ "http://www.163.com"];
[Self.webview Loadrequest:request];
Additional setup after loading the view, typically from a nib.
}

Because you have to add a delegate to WebView to call back those webview functions. Oh, don't forget that.

Children's shoes can also directly in the Xib view file to add a delegate directly to WebView, Ctrl-click the Viewview component and select OutLets below the Delegate drag to File,s Ownver

The following code is then implemented in the WEBVIEWVIEWCONTROLLER.M file

-(void) Webviewdidstartload: (UIWebView *) WebView
{
NSLog (@ "load ing");
[Activityindicationview startanimating];
Activityindicationview.hidden=no;
}

-(void) Webviewdidfinishload: (UIWebView *) webview{
NSLog (@ "Finish ing");
[Activityindicationview stopanimating];
Activityindicationview.hidden=yes;
}

Save run, there is no way to implement the error Oh, children's shoes If you are interested, you can try

Get. Do not understand the place welcome message, together learn common progress.

Getting Started with iOS development-WebView

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.