Create a simple Web browser interface using UIWebView in IOS applications _ios

Source: Internet
Author: User

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:

Copy Code code as follows:

-(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.

Copy Code code as follows:

@interface uiwebviewdemoviewcontroller:uiviewcontroller<uiwebviewdelegate> {
Iboutlet UIWebView *webview;
Iboutlet Uitextfield *textfield;
Uiactivityindicatorview *activityindicatorview;

}
-(Ibaction) ButtonPress: (ID) sender;
-(void) loadwebpagewithstring: (nsstring*) urlstring;
@end

Use IB to correlate them.

Set UIWebView, Initialize Uiactivityindicatorview:

Copy Code code as follows:

-(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 no additional setup after loading the view from its nib.
}

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.

Copy Code code as follows:

-(void) Webviewdidstartload: (UIWebView *) WebView
{
[Activityindicatorview startanimating];
}
-(void) Webviewdidfinishload: (UIWebView *) WebView
{
[Activityindicatorview stopanimating];
}

The ButtonPress method is simple enough to invoke the Loadwebpagewithstring method that we started to define well:
Copy Code code as follows:

-(Ibaction) ButtonPress: (ID) sender
{
[TextField Resignfirstresponder];
[Self loadWebPageWithString:textField.text];

}

When the request page error occurs, we give a hint:
Copy Code code as follows:

-(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];
}

dynamically acquiring UIWebView height
Monitor the contentsize of the WebView and change the webview frame whenever the contentsize value changes.
Copy Code code as follows:

[Activitywebview.scrollview addobserver:self forkeypath:@ "Contentsize" options:nskeyvalueobservingoptionnew Context:nil];

And then change the WebView frame in the callback method.
Copy Code code as follows:

-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary *) Change context: (void *) Context
{
if ([KeyPath isequaltostring:@ "Contentsize"]) {
Webviewheight = [[Activitywebview stringbyevaluatingjavascriptfromstring:@ "Document.body.offsetHeight"] Floatvalue ];
CGRect newframe = activitywebview.frame;
NewFrame.size.height = Webviewheight;
Activitywebview.frame = Newframe;
[Maintableview Settableheaderview:activitywebview];
}
}

Remember the remove listener when the page disappears, or it will flash back
Copy Code code as follows:

-(void) Viewwilldisappear: (BOOL) antimated{
[Super viewwilldisappear:antimated];
[Activitywebview.scrollview removeobserver:self
forkeypath:@ "Contentsize" context:nil];
}

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.