从 facebook iOS API 文档中,我创建了两种方法来请求图表中的朋友列表或当前登录用户的详细信息。
- (IBAction)showMyFriends:(id)sender {
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[delegate facebook] requestWithGraphPath:@"me/friends" andDelegate:self];
NSLog(@"Getting friends list");
}
- (IBAction)showMyDetails:(id)sender {
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[delegate facebook] requestWithGraphPath:@"me" andDelegate:self];
NSLog(@"Getting my info");
}
到目前为止听起来很合理。响应这些调用的委托方法是:
- (void)request:(FBRequest *)request didLoad:(id)result
{
NSLog(@"Got a request");
// Print out friends
// NSArray * items = [NSArray alloc];
// items = [(NSDictionary *)result objectForKey:@"data"];
// for (int i=0; i<[items count]; i++) {
// NSDictionary *friend = [items objectAtIndex:i];
// long long fbid = [[friend objectForKey:@"id"]longLongValue];
// NSString *name = [friend objectForKey:@"name"];
// NSLog(@"id: %lld - Name: %@", fbid, name);
// }
// Print out self username
NSString *username = [result objectForKey:@"name"];
NSLog(@"Username is %@", username);
helloGreeting.text = [NSString stringWithFormat:@"Hello %@", username];
}
问题:在 中didLoad
,您如何检查当前调用与哪个图形请求相关?例如,在上面的代码中,我要么想打印出朋友列表,要么打印出用户名,所以我想我需要根据请求类型将代码包装在某些 case/switch 语句中。
我在 API 上找不到任何明显的东西,确保只执行相关响应代码的最佳方法是什么?