Cover by Vandad Nahavandipoor

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

2.19. Loading Web Pages with UIWebView

Problem

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

Solution

Use the UIWebView class.

Discussion

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:

loadData:MIMEType:textEncodingName:baseURL:

Loads an instance of NSData into the web view.

loadHTMLString:baseURL:

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.

loadRequest:

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;

@end

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

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required