UIWebView Control Usage

Source: Internet
Author: User

UIWebView is a common control in the iOS SDK and is a built-in browser control that we can use to browse the Web, open the document and wait. This blog mainly explains the UIWebView control to make a simple browser

As follows:


First of all, I'm using visual programming to drag and drop controls directly on the view.

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 UIWEBVIEWCONTROLLER.M:

-(void) loadwebpagewithstring: (nsstring*) urlstring

{

nsurl *url =[nsurl urlwithstring: urlstring];

nsurlrequest *request =[nsurlrequestrequestwithurl: url];

[_webviewloadrequest: 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, textfiled for the address bar, Activityindicatorview for loading animations, ButtonPress click events for buttons.

#import<UIKit/UIKit.h>


@interface Mhtviewcontroller: Uiviewcontroller<uiwebviewdelegate>

@property (nonatomic,retain) Uiactivityindicatorview *activityindicatorview;

@property (weak,nonatomic) iboutlet uitextfield * TextField;

@property (weak,nonatomic) iboutlet uiwebview *webview;

-(ibaction) ButtonPress: (UIButton *) sender;

-(void) loadwebpagewithstring: (nsstring*) urlstring;

@end


set UIWebView, initialize Uiactivityindicatorview:

-(void) viewdidload

{

[superviewdidload];

_webview. Scalespagetofit =YES;

_webview. delegate =self;

_activityindicatorview = [[uiactivityindicatorviewalloc]

initwithframe : cgrectmake(0.0f,0.0f, 32.0f, 32.0f )] ;

[_activityindicatorviewsetcenter: self. View . Center ] ;

[_activityindicatorviewsetactivityindicatorviewstyle: Uiactivityindicatorviewstylewhite];

[self. View addsubview : _activityindicatorview];

[self buttonpress:nil];

}

UIWebView has the following principal methods:

1.

-(BOOL) WebView: (uiwebview *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: (uiwebviewnavigationtype) Navigationtype;

Gets notified when a page view is instructed to load content.

2,-(void) Webviewdidstartload: (UIWebView *) WebView; The method is executed when the load is started.

3,-(void) Webviewdidfinishload: (UIWebView *) WebView; The method is executed when the load is complete.

4,-(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

{

[_activityindicatorviewstartanimating];

}

-(void) Webviewdidfinishload: (uiwebview *) WebView

{

[_activityindicatorviewstopanimating];

}

ButtonPress method, call us to start defining the Loadwebpagewithstring method on the line:

-(ibaction) ButtonPress: (UIButton *) sender

{

[_textfieldresignfirstresponder];

[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 = [[uialertviewalloc] initwithtitle : @"" message: [Error localizeddescription] delegate:nil cancelbuttontitle:nilotherbuttontitles:@ "OK" ,nil];

[Alterviewshow];

[Alterviewrelease];

}


attached:

:


Navigation buttons

Back to previous page

Uibarbuttonitem * BTN1 = [[uibarbuttonitemalloc]initwithimage: [UIImage imagenamed:@ "Btn_navigationbar_back1"]style: Uibarbuttonitemstyleplain target: self action:@ Selector(back:)];

// pause button

uibarbuttonitem * btn2 = [[uibarbuttonitemalloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemstoptarget: Self action:@selector(stop:)];

self. Navigationitem. Leftbarbuttonitems =@[btn1,btn2];

//Enter the next page button

uibarbuttonitem * btn3 = [[uibarbuttonitemalloc] Initwithimage: [UIImageimagenamed:@ "Btn_navigationbar_back" ]style:uibarbuttonitemstyleplain target: Self action:@selector(go:)];

// refresh button

uibarbuttonitem * btn4 = [[uibarbuttonitemalloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemrefreshtarget:self action:@selector(refresh:)];

self. Navigationitem. Rightbarbuttonitems =@[btn3, Btn4];

[btn1release];

[btn2release];

[Btn3release];

[Btn4release];

//The method is implemented separately:

[WebView GoBack]; // return

[WebView GoForward]; // forward

[WebView Reload]; // Reload Data

[WebView stoploading]; // Stop loading Data

Property:

Self . Opaque = NO  ; // set whether opaque

Self . Scalespagetofit = YES ; automatically scale the page to fit the screen



UIWebView Control Usage

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.