2.24. 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
NSDatainto the web view.loadHTMLString:baseURL:Loads an instance of
NSStringinto 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>@interfaceLoading_Web_Pages_with_UIWebViewViewController:UIViewController@property(nonatomic,strong)UIWebView*myWebView;@end
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{[superviewDidLoad];self.view.backgroundColor=[UIColorwhiteColor];self.myWebView=[[UIWebViewalloc]initWithFrame:self.view.bounds];[self.viewaddSubview: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access