2.19. Loading Web Pages with UIWebView


You want to load a web page dynamically right inside your iOS app.


Use the UIWebView class.


A web view is what the Safari browser uses on iOS to load web content. You have the whole power of Safari in your iOS apps through the UIWebView class. All you have to do is to place a web view on your UI and use one of its loading methods:


Loads an instance of NSData into the web view.


Loads an instance of NSString into the web view. The string should be a valid HTML, or in other words, something that a web browser can render.


Loads an instance of NSURLRequest. This is useful when you want to load the contents of a remote URL into a web view inside your application.

Let’s see an example. We’ll start with the header file of our view controller:

#import <UIKit/UIKit.h>

@interface Loading_Web_Pages_with_UIWebViewViewController
           : UIViewController

@property (nonatomic, strong) UIWebView *myWebView;


And as you know, we will now synthesize our property:

#import "Loading_Web_Pages_with_UIWebViewViewController.h"

@implementation Loading_Web_Pages_with_UIWebViewViewController

@synthesize myWebView;


Now I would like to load the string iOS 5 Programming Cookbook into the web view. To prove things are working as expected and that our web view is capable of rendering rich text, I will go ahead and make the Programming part bold while leaving the rest of the text ...

Get iOS 5 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.