Transferred from: http://www.cnblogs.com/zhuqil/archive/2011/07/28/2119923.html
UIWebView is one of the most commonly used controls in the iOS SDK. is a built-in browser control that we can use to navigate the Web, open the document, and so on. In this article I will use this control to make a simple browser. Such as:
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, and one UIWebView, with the following layout:
Define related controls in code: WebView is used to display Web pages, TextField for the address bar, Activityindicatorview for loading animations, 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;
@end
|
Use IB to associate them.
Set UIWebView, Initialize Uiactivityindicatorview:
-(void) viewdidload
{
[Super Viewdidload];
Webview.scalespagetofit =yes;
Webview.delegate =self;
Initwithframe:cgrectmake (0.0f, 0.0f, 32.0f, 32.0f)];
[Activityindicatorview SetCenter:self.view.center];
[Self.view Addsubview:activityindicatorview];
[Self buttonpress:nil];
Do any additional setup after loading the view from its nib.
}
UIWebView has the following principal methods:
1,-(void) Webviewdidstartload: (UIWebView *) WebView; The method is executed when the load is started.
2,-(void) Webviewdidfinishload: (UIWebView *) WebView; The method is executed when the load is complete.
3,-(void) WebView: (UIWebView *) WebView didfailloadwitherror: (nserror *) error, load error when the method is executed.
We can place the Activityindicatorview in the previous two delegate methods.
-(void) Webviewdidstartload: (UIWebView *) WebView
{
[Activityindicatorview startanimating];
}
-(void) Webviewdidfinishload: (UIWebView *) WebView
{
[Activityindicatorview stopanimating];
}
The ButtonPress method is simple, just call us to start defining the Loadwebpagewithstring method:
- ( IBAction )buttonPress:( id ) sender { [textField resignFirstResponder]; [ self loadWebPageWithString:textField.text]; } |
When there is an error in the request page, we give you 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, explains the UIWebView methods and properties, I believe that through this example, we should understand the use of UIWebView.
Code: Uiwebviewdemo.zip
iOS Development UIWebView