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 urlwithstring:urlstring];
NSLog (urlstring);
*=[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;
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 I TS nib.
/span>
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*) WebView
{
[Activityindicatorview startanimating];
}
- (void*) WebView
{
[Activityindicatorview stopanimating];
}
The ButtonPress method is simple, just call us to start defining the Loadwebpagewithstring method:
- (IBAction)buttonPress:(id) sender{ [textField resignFirstResponder]; [selfloadWebPageWithString: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
Zhu Qilin
Source:http://zhuqil.cnblogs.com
This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.