以下解决方案对我有用。但是,如果您要存储永久访问令牌,则需要确保用户没有删除应用程序权限,否则会出错。您可以使用 requestWithGraphPath -> "me/permissions" 进行检查。
应用程序初始化函数(例如:didFinishLaunchingWithOptions/或您在其中初始化需要同时为 fbsessiondelegate的 Facebook 对象)
...
NSArray* permissions = [[NSArray alloc] initWithObjects:@"user_likes",@"offline_access", nil];
FBSession*oursession = [[FBSession alloc] initWithPermissions:permissions];
...
FBDidLogin 函数:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
图 api 请求函数示例:
NSUserDefaults *userDefaults =[NSUserDefaults standardUserDefaults];
NSString *key = [userDefaults stringForKey:@"FBAccessTokenKey"];
FBRequest* ourcon = [[FBRequest alloc] initWithSession:oursession graphPath:@"me/likes" parameters:params HTTPMethod:@"GET"];
[ourcon startWithCompletionHandler: ^(FBRequestConnection *connection, id<FBGraphUser> result, NSError *error){
if(error)
{
//NSLog(error.code);
return;
}
NSArray* collection = (NSArray*)[result data];
NSLog(@"You have %d like", [collection count]);
NSDictionary* name = [collection objectAtIndex:14];
NSLog(@"Like Name: %@", [name objectForKey:@"name"]);
}];