Uiwebview for iOS development

Source: Internet
Author: User

Uiwebview is the most common control in ios sdk. Is a built-in browser control, which can be used to browse webpages, open documents, and so on. This articleArticleI will use this control to make a simple browser. For example:

We createWindow-based applicationProgramName: uiwebviewdemo

The loadrequest of uiwebview can be used to load a URL. It requires an nsurlrequest parameter. We define a method to load a URL. Define the following method in uiwebviewdemoviewcontroller:

-(Void) Loadwebpagewithstring :( nsstring*) Urlstring
{
Nsurl*URL=[Nsurl urlwithstring: urlstring];
Nslog (urlstring );
Nsurlrequest*Request=[Nsurlrequest requestwithurl: url];
[Webview loadrequest: request];
}

Place three controls on the interface, one textfield, one button, and one uiwebview. The layout is as follows:

InCodeDefines related controls: webview is used to display web pages, textfield is used for the address bar, activityindicatorview is used to load animations, and buttonpress is used for button click events.

 
@ Interface uiwebviewdemoviewcontroller: uiviewcontroller <uiwebviewdelegate> {iboutlet uiwebview * webview; iboutlet uitextfield * textfield; vertex * vertex;}-(ibaction) buttonpress :( ID) sender;-(void) loadwebpagewithstring :( nsstring *) urlstring; @ end

Use IB to associate them.

Set uiwebview and initializeUiactivityindicatorview:

 -  (  Void ) Viewdidload
{
[Super viewdidload];
Webview. scalespagetofit = Yes;
Webview. Delegate = Self;
Activityindicatorview = [[Uiactivityindicatorview alloc]
Initwithframe: cgrectmake ( 0.0f , 0.0f , 32.0f , 32.0f )];
[Activityindicatorview setcenter: Self. View. Center];
[Activityindicatorview setactivityindicatorviewstyle: uiactivityindicatorviewstylewhite];
[Self. View addsubview: activityindicatorview];
[Self buttonpress: Nil];
// Do any additional setup after loading the view from Its nib.
}

Uiwebview mainly includes the following delegate methods:

1.-(void) webviewdidstartload :( uiwebview *) webview; this method is executed when loading starts.
2.-(void) webviewdidfinishload :( uiwebview *) webview; this method is executed when loading is complete.
3.-(void) webview :( uiwebview *) webview didfailloadwitherror :( nserror *) error; execute this method when loading an error.

We can place activityindicatorview in the previous two delegate methods.

 
-(Void) Webviewdidstartload :( uiwebview*) Webview
{
[Activityindicatorview startanimating];
}
-(Void) Webviewdidfinishload :( uiwebview*) Webview
{
[Activityindicatorview stopanimating];
}

The buttonpress method is very simple. Just call the loadwebpagewithstring method we have defined:

 
-(Ibaction) buttonpress :( ID) sender {[textfield resignfirstresponder]; [self loadwebpagewithstring: textfield. Text];}

When an error occurs on the request page, the following message is displayed:

 
-(Void) webview :( uiwebview *) webview preview :( nserror *) error {uialertview * alterview = [[uialertview alloc] initwithtitle: @ "" message: [error localizeddescription] delegate: nil cancelbuttontitle: Nil otherbuttontitles: @ "OK", nil]; [alterview show]; [alterview release];}

Summary:This article illustrates the methods and attributes of uiwebview by implementing a simple browser. I believe that through this example, we should understand the use of uiwebview.

Code:Uiwebviewdemo.zip

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.