I am using facebook latest iphone sdk, I am making multiple request to facebook from different places in my app. For all of them didReceiveResponse and didLoad methods get called, it is very difficult to find out from didLoad method that for what request this response was so I am wondering if didReceiveResponse can help, can i retrieve some information in this method which will tell me 开发者_开发问答what was the request for which I have got the response.
The way I do it is pretty much the same way Ziminji does it, but in didLoad method:
- (void)request:(FBRequest *)request didLoad:(id)result
{
NSLog(@"Facebook request %@ loaded", [request url]);
//handling a user info request, for example
if ([[request url] rangeOfString:@"/me"].location != NSNotFound)
{
/* handle user request in here */
}
}
So basically you need only to check the url you sent the request to, and you can also check the parameters for that request. Then you can differentiate one from another.
All I am doing here is I am checking for some unique attribute in the response and relating it to the request, I know this is not the best way to do is, but this is what I have found so far, please let me know if any one is doing it any different
You could try something like the following:
- (void) request: (FBRequest *)request didReceiveResponse: (NSURLResponse *)response {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if (statusCode == 200) {
NSString *url = [[response URL] absoluteString];
if ([url rangeOfString: @"me/feed"].location != NSNotFound) {
NSLog(@"Request Params: %@", [request params]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Facebook" message: @"Message successfully posted on Facebook." delegate: nil cancelButtonTitle: @"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
}
If you retain the request object as a property of your request delegate when you create it, you can check to see if it matches on the delegate method calls. For example:
- (void)queryForUserInfo {
self.userInfoRequest = [facebook requestWithGraphPath:@"me" andDelegate:self];
}
#pragma mark <FBRequestDelegate>
- (void)request:(FBRequest *)request didLoad:(id)result {
if (request == self.userInfoRequest) {
[self handleUserInfoResult:result];
}
}
精彩评论