• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Web View

The Web View

This chapter wraps up its discussion of controls by discussing the web view. The UIWebView is the class you use to add a web browser to your application. It’s based upon the same code foundation as Safari, and so you can use CSS and JavaScript. Using the web view can be easy, or more difficult, depending upon how much you wish your application to interact with the browser. In this chapter you keep it simple.

UIWebView

The UIWebView is responsible for the web view’s display. It is an easy means of embedding web content in your application. The loadRequest: method is how you load web content. You can check on the control’s progress loading a resource using the loading property. You might also wish moving forward or backward through the user’s browsing history; you do this using the goBack and goForward methods. And of course, you can first check to see if the control can move backwards or forwards by checking the canGoBack or canGoForward properties.

If you own an iPhone, then I am certain you have noticed that you can tap telephone numbers in the Safari browser and it automatically dials the number. The UIWebView automatically implements this behavior unless you specifically inform it not to by setting the detectsPhoneNumbers property to NO. Another method that is subtle, yet powerful is the stringByEvaluatingJavaScriptFromString: method.

– (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script

Why is this method so powerful? The stringByEvaluatingJavaScriptFromString: method allows evaluating any JavaScript string. You can access a page’s Document Object Model (DOM) through JavaScript, and so you can manipulate an HTML page’s content.

The HTML DOM is a W3C standard for manipulating HTML documents. HTML DOM

is outside this book’s scope, but for more information refer to the W3C School’s HTML DOM Tutorial, available online at www.w3schools.com. If you wish programming using the web browser you would be well served by learning the HTML DOM. The following Try This example, illustrates using the stringByEvaluatingJavaScriptFromString: method to print a page’s HTML content.You navigate to a specific page by using the UIWebView’s loadRequest method.

– (void) loadRequest: (NSURLRequest *) request

The loadRequest method takes a URL request as a parameter and navigates to the resource represented by the underlying URL. A NSURL class wraps the NSURLRequest’s underlying URL. Both the NSURLRequest and NSURL are part of the Foundation framework. You use both in the next Try This example application.

 

UIWebViewDelegate

UIWebViews can also have a delegate. You create a delegate for a UIWebView by creating a class that adopts the UIWebViewDelegate protocol and then assigning the UIWebView’s delegate property to the custom class. The UIWebViewDelegate handles key events when loading a web page. When an event occurs, the web view calls the appropriate delegate method. For instance, when a web view is about to load a page it calls the webView:should StartLoadWithRequest:navigationType: method.

  • (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURL Request *)

request navigationType:(UIWebViewNavigationType)navigationType

When a web view starts loading a page it calls the webViewDidStartLoad: method.

  • (void)webViewDidStartLoad:(UIWebView *)webView

 

When a web view finishes loading a page it calls the webViewDidFinishLoad: method, unless an error occurs, and instead it calls the webView:didFailLoadWithError: method.

  • (void)webViewDidFinishLoad:(UIWebView *)webView
  • (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

 

Creating a Simple Web Browser

1. Create a new View-based Application named MyWeb.
2. Create a new class called MyWebViewDelegate and have it adopt the UIWebViewDelegate protocol (Listings 12-10 and        12-11).
3. Open MyWebViewController.xib in Interface Builder.
4. Add a Web View from the Library to the canvas. Also add a text field and a button

 

 Slide119

 

Figure 12-29 A simple web browser in Interface Builder

  1. Change the text field’s keyboard type to URL.
  2. Save and exit Interface
  3. Open MyWebViewController and add IBOutlets for the text field and the web view (Listings 12-12 and 12-13). Add and IBAction called Add the MyWebViewDelegate as a property and implement the viewDidLoad method.
  4. Save and return to Interface
  5. Connect the button’s Touch Up Inside action to the changeLocation Connect the text field and web view to their respective outlets.
  6. Save and exit Interface
  7. Build and
  8. Type http://www.apple.com in the text field and tap GO and the web page loads in the web view (Figure 12-30). The web page’s HTML is also logged to the debugger console (not provided as a listing due to its length).

 

 

Slide120

 

Figure 12-30 A simple web browser displaying Apple’s home page

 

Listing 12-10 MyWebViewDelegate.h

#import <Foundation/Foundation.h>

@interface MyWebViewDelegate : NSObject <UIWebViewDelegate> {

}

@end

Listing 12-11 MyWebViewDelegate.m

#import “MyWebViewDelegate.h”

@implementation MyWebViewDelegate

– (void)webViewDidFinishLoad: (UIWebView *) webView { NSLog(@”%@”, [webView stringByEvaluatingJavaScriptFromString:

@”document.documentElement.textContent”]);

}

@end

Listing 12-12 MyWebViewController.h

#import <UIKit/UIKit.h>

#import “MyWebViewDelegate.h”

@interface MyWebViewController : UIViewController { IBOutlet UITextField * myTextField;

IBOutlet UIWebView * myWebView; MyWebViewDelegate * myWebViewDelegate;

}

@property(nonatomic, retain) UIWebView * myWebView;

@property(nonatomic, retain) UITextField * myTextField;

@property(nonatomic, retain) MyWebViewDelegate * myWebViewDelegate;

– (IBAction) changeLocation: (id) sender;

@end

Listing 12-13 MyWebViewController.m

#import “MyWebViewController.h”

@implementation MyWebViewController

@synthesize myWebView;

@synthesize myTextField;

@synthesize myWebViewDelegate;

  • (void) viewDidLoad {

myWebViewDelegate = [[MyWebViewDelegate alloc] init]; myWebView.delegate = myWebViewDelegate;

}

  • (void)dealloc { delegate = nil; [myWebViewDelegate release]; [myTextField release]; [myWebView release];

[super dealloc];

}

  • (IBAction) changeLocation: (id) sender { [myTextField resignFirstResponder];

NSURL * url = [NSURL URLWithString: myTextField.text]; NSURLRequest * request = [NSURLRequest requestWithURL:url]; [myWebView loadRequest:request];

} @end

Summary
Review Date
Reviewed Item
Web View
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Web View"

Leave a Message

Your email address will not be published. Required fields are marked *

Site Disclaimer, Copyright © 2016 - All Rights Reserved.