Whenever a request is made via the requestWithGraphPath:andDelegate: method, a delegate must be specified in order to handle the response from the Facebook iOS SDK. It’s important to note that the Facebook iOS SDK calls the methods of the FBRequestDelegate in the following order. First, before the request is made to the Facebook servers, the requestLoading: method is called. When the Facebook servers send a response, the request:didReceiveResponse: method is called. Next, before the Facebook iOS SDK starts to handle the response, the request:didLoadRawResponse: method is called to give the delegate the chance to process the response data itself. Finally, the request:didLoad: method is called with the response data stored in anObjective-C data type. If there was a problem with the request, then the request:didFailWithError: method is called.ios
If we take a look at the request:didLoad: method in the FacebookViewController, we see the following:json
- (void)request:(FBRequest *)request didLoad:(id)result { NSLog(@"didLoad:"); [items release]; items = [[(NSDictionary*)result objectForKey:@"data"] retain]; [self.tableView reloadData]; }
For the majority of requests that you make from the Facebook social graph, the response is going to be a dictionary with one key/value pair, where the key is data, and the value is an array or list of items. app
The Facebook iOS SDK returns the image as bytes within an NSData object. Next, we create an image from this object, as shown here in FriendTableViewCell’s facebookRequestDidComplete: method:ide
(void)facebookRequestDidComplete:(NSNotification*)notification { if (YES == [self.requestPath isEqualToString:[notification.userInfo objectForKey:@"path"]]) { UIImage *image = [UIImage imageWithData:(NSData*)[notification.userInfo objectForKey:@"result"]]; [self setNeedsLayout]; } }
Paging Graph Responsesthis
One interesting thing that we’d like to point out is that you can limit the number of items you get in a response from the Facebook graph when making a request. This is accomplished by adding the limit parameter to the request. You can do this via the requestWithGraphPath:andDelegate: method:spa
[facebook requestWithGraphPath:@"me/friends?limit=3" andDelegate:self];
You can also do this via requestWithGraphPath:andParams:andDelegate: by creating a dictionary of parameters. For each object in the dictionary, the key is the name of the parameter—limit, in this case—and the value is a string representation of the value. Here is what the code looks like:rest
NSMutableDictionary *params = [NSMutableDictionary dictionary]; [params setObject:@"3" forKey:@"limit"]; [params setObject:@"5" forKey:@"offset"]; [facebook requestWithGraphPath:@"me/feed" andParams:params andDelegate:self];
You can also specify that you would like to retrieve items from a given starting point or offset by using the offset parameter:code
[facebook requestWithGraphPath:@"me/friends?limit=3&offset=5" andDelegate:self];
Alternatively, you can also accomplish this same task by passing in a dictionary of parameters:
orm
NSMutableDictionary *params = [NSMutableDictionary dictionary]; [params setObject:@"3" forKey:@"limit"]; [params setObject:@"5" forKey:@"offset"]; [facebook requestWithGraphPath:@"me/feed" andParams:params andDelegate:self];
The FBRequest Class
server
The Facebook iOS SDK class that actually makes the requests and handles the responses is FBRequest. The Facebook iOS SDK takes advantage of the fact that the underlying base URL that it needs to use to make requests never changes: https://graph.facebook.com. The SDK also knows that certain parameters for a request never change. This means that you only need to provide the request methods with those parts of the Graph Path that change based on the context of your application.
So, if we were to construct the full URL to request the current user’s friends from the Facebook graph, it would look as follows:
https://graph.facebook.com/me/friends?sdk=ios&sdk_version=2&access_token=<your token>&format=json
We are using the Facebook iOS SDK, so we only need to give the SDK the Graph Path "me/friends" (along with any other parameters to control the request) since the underlying SDK classes construct the full URL for us.
The final request is constructed in FBRequest’s connect method, so it is worth studying. This is where the actual request is made, and the JSON response is handled. The JSON response is handled in the method handleResponseData:(NSData *)data. In the case of a request for a user’s friends, this is what the JSON response looks like:
{"data":[{"name":"John Dor","id":"<some ID>"}]}
It is also worth studying FBRequest’s getRequestWithParams:httpMethod:delegate:requestURL: method and Facebook’s openUrl:params:httpMethod:delegate:.
Error Handling
There is no right or wrong way to handle errors from the Facebook iOS SDK. It all depends on you and your application. We encourage you to implement any delegate methods that notify you of errors, so that you can take the appropriate action and notify the user or update your application’s user interface. With respect to what we have covered for Facebook in this chapter, be sure to implement FBRequestDelegate’s request:didFailWithError: method.