iOS Development UIWebView

Source: Internet
Author: User

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];     [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

iOS Development UIWebView

Related Article

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.