1、登录操作
进行登录操作的时候,需要获取用户的权限,其中public_profile,email,user_friends不需要通过facebook的审核就可以直接获得
#import <FBSDKLoginKit/FBSDKLoginKit.h>
AppController * controller=[AppController getSharedInstance];
FBSDKLoginManager * manager=[[FBSDKLoginManager alloc] init];
[manager logInWithReadPermissions: @[@"public_profile",@"email",@"user_friends"]
fromViewController:controller.viewController
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error)
{
//do something;
}else if (result.isCancelled)
{
//do something;
}else
{
//do something;
}
}];
2、登出
调用FBSDKLoginManager的logout 函数。
3、判断是否登录成功
if ([FBSDKAccessToken currentAccessToken])
{
return true;
}else
{
return false;
}
4、监听登录用户变化
[[NSNotificationCenter defaultCenter] addObserverForName:FBSDKAccessTokenDidChangeNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:
^(NSNotification *notification) {
if (notification.userInfo[FBSDKAccessTokenDidChangeUserID]) {
// Handle user change
CCLOG("FB access tokenDidChanged");
}
}];
5、获取用户信息
获取用户信息需要用到facebook的open graph api。这里只获取了用户的id和头像,如果需要获取更多的信息可以在params内添加
NSDictionary*params= @{@"fields":@"id,name"};
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:[FBSDKAccessToken currentAccessToken].userID
parameters:pParams
HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
{
if(error)
{
//do something
}else
{
NSLog(@"user info%@",result);
}
}];
6、获取好友信息
获取好友信息同样是通过open graph api获取。
注意的是这里获取的好友信息只能获取登录了该应用的好友。而且在测试状态下,非应用开发者的账号需要先到facebook的后台设置为测试账号才能进行登录操作。
NSDictionary*params= @{@"fields":@"id,name"};
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:[NSString stringWithFormat:@"%@/friends",[FBSDKAccessToken currentAccessToken].userID]
parameters:params
HTTPMethod:@"GET" ];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
if(error)
{
//do something
}else
{
NSLog(@"friends:%@",result);
}
}];