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