UIWebView

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:

@​i​n​t​e​r​f​a​c​e​ ​B​N​R​W​e​b​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​:​ ​N​S​O​b​j​e​c​t​
@​i​n​t​e​r​f​a​c​e​ ​B​N​R​W​e​b​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​:​ ​U​I​V​i​e​w​C​o​n​t​r​o​l​l​e​r​

@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​)​ ​N​S​U​R​L​ ​*​U​R​L​;​

@​e​n​d​

In BNRWebViewController.m, write the following implementation.

@​i​m​p​l​e​m​e​n​t​a​t​i​o​n​ ​B​N​R​W​e​b​V​i​e​w​C​o​n​t​r​o​l​l​e​r​

-​ ​(​v​o​i​d​)​l​o​a​d​V​i​e​w​
{​
 ​ ​ ​ ​U​I​W​e​b​V​i​e​w​ ​*​w​e​b​V​i​e​w​ ​=​ ​[​[​U​I​W​e​b​V​i​e​w​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​
 ​ ​ ​ ​w​e​b​V​i​e​w​.​s​c​a​l​e​s​P​a​g​e​T​o​F​i​t​ ​=​ ​Y​E​S​;​
 ​ ​ ​ ​s​e​l​f​.​v​i​e​w​ ​=​ ​w​e​b​V​i​e​w​;​
}​

-​ ​(​v​o​i​d​)​s​e​t​U​R​L​:​(​N​S​U​R​L​ ​*​)​U​R​L​
{​
 ​ ​ ​ ​_​U​R​L​ ​=​ ​U​R​L​;​
 ​ ​ ​ ​i​f​ ​(​_​U​R​L​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​U​R​L​R​e​q​u​e​s​t​ ​*​r​e​q​ ​=​ ​[​N​S​U​R​L​R​e​q​u​e​s​t​ ​r​e​q​u​e​s​t​W​i​t​h​U​R​L​:​_​U​R​L​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​(​U​I​W​e​b​V​i​e​w​ ​*​)​s​e​l​f​.​v​i​e​w​ ​l​o​a​d​R​e​q​u​e​s​t​:​r​e​q​]​;​
 ​ ​ ​ ​}​
}​

@​e​n​d​

In BNRCoursesViewController.h, add a new property to hang on to an instance of BNRWebViewController.

@​c​l​a​s​s​ ​B​N​R​W​e​b​V​i​e​w​C​o​n​t​r​o​l​l​e​r​;​

@​i​n​t​e​r​f​a​c​e​ ​B​N​R​C​o​u​r​s​e​s​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​:​ ​U​I​T​a​b​l​e​V​i​e​w​C​o​n​t​r​o​l​l​e​r​

@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​)​ ​B​N​R​W​e​b​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​*​w​e​b​V​i​e​w​C​o​n​t​r​o​l​l​e​r​;​

@​e​n​d​

In BNRAppDelegate.m, import the header for BNRWebViewController, create an instance of BNRWebViewController, and set it as the BNRWebViewController of the BNRCoursesViewController.

#​i​m​p​o​r​t​ ​"​B​N​R​W​e​b​V​i​e​w​C​o​n​t​r​o​l​l​e​r​.​h​"​

@​i​n​t​e​r​f​a​c​e​ ​B​N​R​C​o​u​r​s​e​s​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​(​)​

@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​)​ ​N​S​U​R​L​S​e​s​s​i​o​n​ ​*​s​e​s​s​i​o​n​;​
@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​,​ ​c​o​p​y​)​ ​N​S​A​r​r​a​y​ ​*​c​o​u​r​s​e​s​;​

@​e​n​d​

@​i​m​p​l​e​m​e​n​t​a​t​i​o​n​ ​B​N​R​A​p​p​D​e​l​e​g​a​t​e​

-​ ​(​B​O​O​L​)​a​p​p​l​i​c​a​t​i​o​n​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​l​i​c​a​t​i​o​n​
 ​ ​ ​ ​d​i​d​F​i​n​i​s​h​L​a​u​n​c​h​i​n​g​W​i​t​h​O​p​t​i​o​n​s​:​(​N​S​D​i​c​t​i​o​n​a​r​y​ ​*​)​l​a​u​n​c​h​O​p​t​i​o​n​s​
{​
 ​ ​ ​ ​s​e​l​f​.​w​i​n​d​o​w​ ​=​ ​[​[​U​I​W​i​n​d​o​w​ ​a​l​l​o​c​]​ ​i​n​i​t​W​i​t​h​F​r​a​m​e​:​[​[​U​I​S​c​r​e​e​n​ ​m​a​i​n​S​c​r​e​e​n​]​ ​b​o​u​n​d​s​]​]​;​

 ​ ​ ​ ​B​N​R​C​o​u​r​s​e​s​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​*​c​v​c​ ​=​
 ​ ​ ​ ​ ​ ​ ​ ​[​[​B​N​R​C​o​u​r​s​e​s​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​a​l​l​o​c​]​ ​i​n​i​t​W​i​t​h​S​t​y​l​e​:​U​I​T​a​b​l​e​V​i​e​w​S​t​y​l​e​P​l​a​i​n​]​;​

 ​ ​ ​ ​U​I​N​a​v​i​g​a​t​i​o​n​C​o​n​t​r​o​l​l​e​r​ ​*​m​a​s​t​e​r​N​a​v​ ​=​
 ​ ​ ​ ​ ​ ​ ​ ​[​[​U​I​N​a​v​i​g​a​t​i​o​n​C​o​n​t​r​o​l​l​e​r​ ​a​l​l​o​c​]​ ​i​n​i​t​W​i​t​h​R​o​o​t​V​i​e​w​C​o​n​t​r​o​l​l​e​r​:​c​v​c​]​;​

 ​ ​ ​ ​B​N​R​W​e​b​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​*​w​v​c​ ​=​ ​[​[​B​N​R​W​e​b​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​
 ​ ​ ​ ​c​v​c​.​w​e​b​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​=​ ​w​v​c​;​

 ​ ​ ​ ​s​e​l​f​.​w​i​n​d​o​w​.​r​o​o​t​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​=​ ​m​a​s​t​e​r​N​a​v​;​

 ​ ​ ​ ​s​e​l​f​.​w​i​n​d​o​w​.​b​a​c​k​g​r​o​u​n​d​C​o​l​o​r​ ​=​ ​[​U​I​C​o​l​o​r​ ​w​h​i​t​e​C​o​l​o​r​]​;​
 ​ ​ ​ ​[​s​e​l​f​.​w​i​n​d​o​w​ ​m​a​k​e​K​e​y​A​n​d​V​i​s​i​b​l​e​]​;​
 ​ ​ ​ ​r​e​t​u​r​n​ ​Y​E​S​;​
}​

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

#​i​m​p​o​r​t​ ​"​B​N​R​W​e​b​V​i​e​w​C​o​n​t​r​o​l​l​e​r​.​h​"​

@​i​m​p​l​e​m​e​n​t​a​t​i​o​n​ ​B​N​R​C​o​u​r​s​e​s​V​i​e​w​C​o​n​t​r​o​l​l​e​r​

-​ ​(​v​o​i​d​)​t​a​b​l​e​V​i​e​w​:​(​U​I​T​a​b​l​e​V​i​e​w​ ​*​)​t​a​b​l​e​V​i​e​w​
 ​ ​ ​ ​d​i​d​S​e​l​e​c​t​R​o​w​A​t​I​n​d​e​x​P​a​t​h​:​(​N​S​I​n​d​e​x​P​a​t​h​ ​*​)​i​n​d​e​x​P​a​t​h​
{​
 ​ ​ ​ ​N​S​D​i​c​t​i​o​n​a​r​y​ ​*​c​o​u​r​s​e​ ​=​ ​s​e​l​f​.​c​o​u​r​s​e​s​[​i​n​d​e​x​P​a​t​h​.​r​o​w​]​;​
 ​ ​ ​ ​N​S​U​R​L​ ​*​U​R​L​ ​=​ ​[​N​S​U​R​L​ ​U​R​L​W​i​t​h​S​t​r​i​n​g​:​c​o​u​r​s​e​[​@​"​u​r​l​"​]​]​;​

 ​ ​ ​ ​s​e​l​f​.​w​e​b​V​i​e​w​C​o​n​t​r​o​l​l​e​r​.​t​i​t​l​e​ ​=​ ​c​o​u​r​s​e​[​@​"​t​i​t​l​e​"​]​;​
 ​ ​ ​ ​s​e​l​f​.​w​e​b​V​i​e​w​C​o​n​t​r​o​l​l​e​r​.​U​R​L​ ​=​ ​U​R​L​;​
 ​ ​ ​ ​[​s​e​l​f​.​n​a​v​i​g​a​t​i​o​n​C​o​n​t​r​o​l​l​e​r​ ​p​u​s​h​V​i​e​w​C​o​n​t​r​o​l​l​e​r​:​s​e​l​f​.​w​e​b​V​i​e​w​C​o​n​t​r​o​l​l​e​r​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​a​n​i​m​a​t​e​d​:​Y​E​S​]​;​
}​

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset