1.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 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 implementation file of our view controller:
#import "ViewController.h"@interfaceViewController()@property(nonatomic,strong)UIWebView*myWebView;@end@implementationViewController
Now I would like to load the string iOS 7 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 Cookbook part bold
while leaving the rest of the text intact (Figure 1-65):
-(void)viewDidLoad{[superviewDidLoad];self.myWebView=[[UIWebViewalloc]initWithFrame:self.view.bounds];[self.viewaddSubview:self.myWebView];NSString*htmlString=@"<br/>iOS 7 Programming ...
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