Reputation: 2343
I am working on a new project with facebook integration. The app has to make different requests to facebook. The SDK provides one delegate that handles every request the same. Now i need to have a custom delegate to handle certain request different. Could somebody give me a hint on how to accomplish this?
Upvotes: 2
Views: 1513
Reputation: 3931
The Facebook class exposes numerous method which return FBRequest* objects, and each of those methods have an argument for a delegate, which can be any object you like, so long as it conforms to the FBRequestDelegate protocol.
Therefore either, just have a custom class which implements the protocol for each type of request. Or, have one class, which implements the protocol, and inside those methods, you'll need to inspect the FBRequest that you receive to determine what to do. So, for example, if you're calling:
Facebook *fb = [[Facebook alloc] initWithAppId:kYourFacebookAppId];
[params setObject:[NSNumber numberWithBool:YES] forKey:@"PostComment"];
[fb requestWithParams:params andDelegate:delegate];
[fb release];
then in the delegate, you could do something like:
- (void)request:(FBRequest *)request didLoad:(id)result {
// Make a decision based on the request
if ([[request.params objectForKey:@"PostComment"] boolValue]) {
// Here we should post a comment.
}
}
However, I personally wouldn't do that, as it'll be much harder to maintain. Much better would be to write separate classes for each Facebook related task. These classes could inherit from a base FBDelegate class which does common things like error handling.
Upvotes: 4