The UIWebView of iOS development is what this article is about, UIWebView is one of the most commonly used controls in the iOS SDK. is a built-in browser control that we can use to browse Web pages, open documents, and so on. In this article I will use this control to make a simple browser. The following figure:
We create a window-based application program named: Uiwebviewdemo
The UIWebView loadrequest can be used to load a URL address, which requires a nsurlrequest parameter. We define a method to load the URL. Define the following methods in Uiwebviewdemoviewcontroller:
-(void) loadwebpagewithstring: (nsstring*) urlstring{
nsurl *url =[nsurl urlwithstring:urlstring];
NSLog (urlstring);
Nsurlrequest *request =[nsurlrequest Requestwithurl:url];
[WebView loadrequest:request];}
Place 3 controls on the interface, one TextField, one button, one UIWebView, and the layout is as follows:
Define related controls in code: WebView is used to show Web pages, TextField for address bars, Activityindicatorview animations for loading, ButtonPress click events for buttons.
@interface uiwebviewdemoviewcontroller:uiviewcontroller<uiwebviewdelegate> {
iboutlet UIWebView *webView ;
Iboutlet Uitextfield *textfield;
Uiactivityindicatorview *activityindicatorview;
}
-(Ibaction) ButtonPress: (ID) sender;
-(void) loadwebpagewithstring: (nsstring*) urlstring;
Use IB to correlate them.
Set UIWebView, Initialize Uiactivityindicatorview:
-(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];
UIWebView mainly has the following principal methods:
1,-(void) Webviewdidstartload: (UIWebView *) WebView, the method is executed when the load is started.
2,-(void) Webviewdidfinishload: (UIWebView *) WebView, execution of the method when loading is complete.
3,-(void) WebView: (UIWebView *) WebView didfailloadwitherror: (nserror *) Error, execute this method when loading error.
We can place the Activityindicatorview in the previous two delegate methods.
The ButtonPress method is simple enough to invoke the Loadwebpagewithstring method that we started to define well:
-(Ibaction) ButtonPress: (ID) sender
{
[TextField resignfirstresponder];
[Self loadWebPageWithString:textField.text];
}
When the request page error occurs, we give a hint:
-(void) WebView: (UIWebView *) WebView didfailloadwitherror: (nserror *) error
{
Uialertview *alterview = [ Uialertview alloc] initwithtitle:@ "" Message:[error Localizeddescription] Delegate:nil cancelbuttontitle:nil otherbuttontitles:@ "OK", nil];
[Alterview show];
[Alterview release];
}
Summary: This article through the implementation of a simple browser, explained the UIWebView method and attributes, I believe that through this example, should understand the use of UIWebView.
The follow-up will also continue to share knowledge about the UIWebView of iOS development, please pay attention to the cloud Habitat community website, thank you.