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
NSString *urlString = [NSString stringWithFormat:@”http://api.twitter.com/1/users/lookup.json?
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