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


Now I would like to load the string iOS 6 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 intact (Figure 2-70):

- (void)viewDidLoad{
  [super viewDidLoad];

  self.view.backgroundColor = [UIColor whiteColor];

  self.myWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
  [self.view addSubview: ...

Get iOS 6 Programming Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.