Read iOS Programming: The Big Nerd Ranch Guide, 3/e (Big Nerd Ranch Guides) Online
Authors: Aaron Hillegass,Joe Conway
Tags: #COM051370, #Big Nerd Ranch Guides, #iPhone / iPad Programming
In this exercise, you created a storyboard, set up a few view controllers, laid out their interfaces, and created some segues between them. This is the basic idea behind storyboards, and while there are a few more flavors of segues and types of view controllers you can set up, you get the idea. A storyboard replaces lines of code.
For example, the push segues in this application replace this code:
While this seems nice, storyboarding, in our opinion, is not very useful. Let’s go through the pros and cons. First, the pros:
The cons, unfortunately, outweigh the pros:
Overall, storyboards make easy code easier and difficult code more difficult. We won’t be using them in this book, and we do not use them when writing our own applications. It is up to you to decide if a particular application would benefit from storyboarding. Of course, your opinion may change as your project gets more complex. Don’t say we didn’t warn you!
In this chapter, you will lay the foundation of an application that reads the RSS feed from the Big Nerd Ranch Forums (
Figure 25.1
). Forum posts will be listed in a table view, and selecting a post from the table will display it from the site.
Figure 25.1
shows the
Nerdfeed
application at the end of this chapter.
Figure 25.1 Nerdfeed
We will divide the work into two parts. The first is connecting to and collecting data from a web service and using that data to create model objects. The second part is using the
UIWebView
class to display web content.
Figure 25.2
shows an object diagram for
Nerdfeed
.
Figure 25.2 Nerdfeed object diagram
Your handy web browser uses the HTTP protocol to communicate with a web server. In the simplest interaction, the browser sends a request to the server specifying a URL. The server responds by sending back the requested page (typically HTML and images), which the browser formats and displays.
In more complex interactions, browser requests include other parameters, like form data. The server processes these parameters and returns a customized, or dynamic, web page.
Web browsers are widely used and have been around for a long time. So the technologies surrounding HTTP are stable and well-developed: HTTP traffic passes neatly through most firewalls, web servers are very secure and have great performance, and web application development tools have become easy to use.
You can write a client application for iOS that leverages the HTTP infrastructure to talk to a web-enabled server. The server side of this application is a
web service
. Your client application and the web service can exchange requests and responses via HTTP.
Because the HTTP protocol doesn’t care what data it transports, these exchanges can contain complex data. This data is typically in XML or JSON (JavaScript Object Notation) format. If you control the web server as well as the client, you can use any format you like; if not, you have to build your application to use whatever the server supports.
In this chapter, you will create a client application that will make a request to the
smartfeed
web service hosted at
http://forums.bignerdranch.com
. You will pass a number of arguments to this service that determine the format of the data that is returned. This data will be XML that describes the most recent posts at our developer forums.
Create a new
Empty Application
for the
iPad
Device Family. Name this application
Nerdfeed
, as shown in
Figure 25.3
. (If you don’t have an iPad to deploy to, use the iPad simulator.)
Figure 25.3 Creating an iPad Empty Application
Let’s knock out the basic UI before focusing on web services. Create a new
NSObject
subclass and name it
ListViewController
. In
ListViewController.h
, change the superclass to
UITableViewController
.
In
ListViewController.m
, write stubs for the required data source methods so that we can build and run as we go through this exercise.
In
NerdfeedAppDelegate.m
, create an instance of
ListViewController
and set it as the root view controller of a navigation controller. Make that navigation controller the root view controller of the window.
Build and run the application. You should see an empty
UITableView
and a navigation bar.
The
Nerdfeed
application will fetch data from a web server using three handy classes:
NSURL
,
NSURLRequest
, and
NSURLConnection
(
Figure 25.4
).
Figure 25.4 Relationship of web service classes
Each of these classes has an important role in communicating with a web server:
The form of a web service request varies depending on who implements the web service; there are no set-in-stone rules when it comes to web services. You will need to find the documentation for the web service to know how to format a request. As long as a client application sends the server what it wants, you have a working exchange.
The Big Nerd Ranch Forum’s RSS feed wants a URL that looks like this:
You can see that the base URL is
forums.bignerdranch.com
, the web application is
smartfeed
, and there are five arguments. These arguments are required by the
smartfeed
web application.
This is a pretty common form for a web service request. Generally, a request URL looks like this:
At times, you will need to make a string
“
URL-safe.
”
For example, space characters and quotes are not allowed in URLs; They must be replaced with escape-sequences. Here is how that is done.
When the request to the Big Nerd Ranch forums is processed, the server will return XML data that contains the last 20 posts. The
ListViewController
, who made the request, will populate its table view with the titles of the posts.
In
ListViewController.h
, add an instance variable for the connection and one for the data that is returned from that connection. Also add a new method declaration.
An
NSURLConnection
instance can communicate with a web server either synchronously or asynchronously. Because passing data to and from a remote server can take some time, synchronous connections are generally frowned upon because they stall your application until the connection completes. This chapter will teach you how to perform an asynchronous connection with
NSURLConnection
.
When an instance of
NSURLConnection
is created, it needs to know the location of the web application and the data to pass to that web server. It also needs a delegate. When told to start communicating with the web server,
NSURLConnection
will initiate a connection to the location, begin passing it data, and possibly receive data back. It will update its delegate each step of the way with useful information.
In
ListViewController.m
, implement the
fetchEntries
method to create an
NSURLRequest
that connects to
http://forums.bignerdranch.com
and asks for the last 20 posts in RSS 2.0 format. Then, create an
NSURLConnection
that transfers this request to the server.
Kick off the exchange whenever the
ListViewController
is created. In
ListViewController.m
, override
initWithStyle:
.
Build the application to make sure there are no syntax errors.