Examples of IOS development Uiwebview_ios

Source: Internet
Author: User

The UIWebView of iOS development is what this article is about, 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:

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

@interface uiwebviewdemoviewcontroller:uiviewcontroller<uiwebviewdelegate> { 
iboutlet UIWebView *webView ;
Iboutlet Uitextfield *textfield;
Uiactivityindicatorview *activityindicatorview;
}
-(Ibaction) ButtonPress: (ID) sender;
-(void) loadwebpagewithstring: (nsstring*) urlstring;

Use IB to correlate 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]; 

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.

 
 

The ButtonPress method is simple enough to invoke the Loadwebpagewithstring method that we started to define well:

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

When the request page error occurs, we give 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, explained the UIWebView method and attributes, I believe that through this example, should understand the use of UIWebView.

The follow-up will also continue to share knowledge about the UIWebView of iOS development, please pay attention to the cloud Habitat community website, thank you.

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.