iOS Programming – Working with the Network

Modern applications need to connect to the network to read data from web services.  This is one of the strengths of mobile development because it use capable of delivering information specific to the end user.  iOS applications are quite capable of connecting to the network and reading data.  The following example shows my Twitter Profile application.  If you remember in part 1 we created the application but hard coded the values coming back.

Get Data Back from the Service

Generally when you return data from a service its either going to be in JSON (JavaScript Object Notation) or XML format.  My preference for services tends to be JSON because its much lighter on the way down than XML.  The following code shows how to setup a URL and query the Twitter API to get information about a user (in this case its me).

NSString *urlString = [NSString stringWithFormat:@”

screen_name=%@&include_entties=true”, twitterHandle];

NSURL *url = [NSURL URLWithString:urlString];


NSData *data = [NSData dataWithContentsOfURL:url];

Easy.  Just create a string (or you can do it on one line if you like, but Objective-C gets ugly fast).  Call URLWithString to create an instance of NSURL which we will be using to call our service.  The data for this is collected into an instance of NSData which uses the url object to build the data object as it comes back from the request.

Using the data in your program

So you have the data, now we have to use it.  In iOS5 there is a new class, the NSJSONSerialization class, which can read an instance of NSData and wrap it so the JSON can be read very easily.

NSArray *returnResult = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

NSDictionary *profileInfo = [returnResult objectAtIndex:0];

Since Twitter returns to us an array of JSON objects (though it will only ever contain one item) we set our processed JSON to a variable of type NSArray.  As mentioned, we need the very first (and only item) in this array.  This will be a key=>value dictionary which represents the contents of the JSON response.  I have decided, for sake of clarity, to break this read up.  Here is how I got the data out of the NSDictionary (which is one of the weirdest Hashtable types I have ever worked with).

NSNumber *follower_count = [profileInfo objectForKey:@”followers_count”];

NSNumber *tweet_count = [profileInfo objectForKey:@”statuses_count”];

NSNumber *friend_count = [profileInfo objectForKey:@”friends_count”];

NSDictionary *status = [profileInfo objectForKey:@”status”];

NSNumber is effectively a wrapper for int, which we need here because objectForKey returns an object and thus we can’t do a straight assignment.  Also,objectForKey is defined to return id which is a variable type.  I don’t want to say Variant ala VB, but that is what it kind of feels like, though I am told it is closer to dynamic in C#.

Notice that we did return an instance of NSDictionary for the key status, which in the Twitter API is a block of JSON to show the current status for that user.

Assign these to the properties of our TwitterProfile object (below).

TwitterProfile *profile = [[TwitterProfile alloc] init];

profile.followersCount = [follower_count intValue];

profile.friendsCount = [friend_count intValue];

profile.tweetCount = [tweet_count intValue];

profile.currentStatus = [status objectForKey:@”text”];

Return this and we are good to go.


Accessing data from iOS is easy and the effect is immense.  Data downloaded from the web, especially data pertaining to the end user can make your app more lively and more personable and thus, hopefully more popular.  Thanks


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s