更新时间:2023-09-10 17:57:10
以下解决方案对我有用.但是,如果您要存储永久访问令牌,则需要确保用户没有删除应用程序权限,否则会出错.您可以使用 requestWithGraphPath -> "me/permissions" 进行检查.
Following solution worked for me. But if you're storing a permanent access token, you need to be sure user not removed application permissions otherwise it will give an error. And you can check that with requestWithGraphPath -> "me/permissions".
应用程序初始化函数(例如:didFinishLaunchingWithOptions/或您初始化 Facebook 对象的位置同时需要为 fbsessiondelegate)
Application Init Function (e.g.:didFinishLaunchingWithOptions/or where you init your Facebook object which needs to be fbsessiondelegate in the mean time)
...
NSArray* permissions = [[NSArray alloc] initWithObjects:@"user_likes",@"offline_access", nil];
FBSession*oursession = [[FBSession alloc] initWithPermissions:permissions];
...
FBDidLogin 函数:
FBDidLogin function:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
图api请求函数示例:
Example graph api request function:
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"]);
}];