In addition to its title, each course dictionary also keeps a URL string that points to its web page. It would be neat if Nerdfeed could open up Safari to open that URL. It would be even neater if Nerdfeed could open the web page without having to leave Nerdfeed to open Safari. Good news – it can use the class UIWebView.
Instances of UIWebView render web content. In fact, the Safari application on your device uses a UIWebView to render its web content. In this part of the chapter, you will create a view controller whose view is an instance of UIWebView. When one of the items is selected from the table view of courses, you will push the web view’s controller onto the navigation stack and have it load the URL string stored in the NSDictionary.
Create a new NSObject subclass and name it BNRWebViewController. In BNRWebViewController.h, add a property and change the superclass to UIViewController:
@interface BNRWebViewController : NSObject@interface BNRWebViewController : UIViewController @property (nonatomic) NSURL *URL; @end
In BNRWebViewController.m, write the following implementation.
@implementation BNRWebViewController - (void)loadView { UIWebView *webView = [[UIWebView alloc] init]; webView.scalesPageToFit = YES; self.view = webView; } - (void)setURL:(NSURL *)URL { _URL = URL; if (_URL) { NSURLRequest *req = [NSURLRequest requestWithURL:_URL]; [(UIWebView *)self.view loadRequest:req]; } } @end
In BNRCoursesViewController.h, add a new property to hang on to an instance of BNRWebViewController.
@class BNRWebViewController; @interface BNRCoursesViewController : UITableViewController @property (nonatomic) BNRWebViewController *webViewController; @end
In BNRAppDelegate.m, import the header for BNRWebViewController, create an instance of BNRWebViewController, and set it as the BNRWebViewController of the BNRCoursesViewController.
#import "BNRWebViewController.h" @interface BNRCoursesViewController () @property (nonatomic) NSURLSession *session; @property (nonatomic, copy) NSArray *courses; @end @implementation BNRAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; BNRCoursesViewController *cvc = [[BNRCoursesViewController alloc] initWithStyle:UITableViewStylePlain]; UINavigationController *masterNav = [[UINavigationController alloc] initWithRootViewController:cvc]; BNRWebViewController *wvc = [[BNRWebViewController alloc] init]; cvc.webViewController = wvc; self.window.rootViewController = masterNav; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }
(Note that you are instantiating the BNRWebViewController in the application delegate in preparation for the next chapter, where you will use a UISplitViewController to present view controllers on the iPad.)
In BNRCoursesViewController.m, import the header files for BNRWebViewController and then implement tableView:didSelectRowAtIndexPath: to configure and push the webViewController onto the navigation stack when a row is tapped.
#import "BNRWebViewController.h" @implementation BNRCoursesViewController - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *course = self.courses[indexPath.row]; NSURL *URL = [NSURL URLWithString:course[@"url"]]; self.webViewController.title = course[@"title"]; self.webViewController.URL = URL; [self.navigationController pushViewController:self.webViewController animated:YES]; }
Build and run the application. You should be able to select one of the courses, and it should take you to a new view controller that displays the web page for that course.