本文档主要阐述游戏中接入Facebook平台的相关内容,使得接入过程尽可能详尽而简单。
Facebook SDK中较为核心的一个类是FBSession,该类主要用于用户验证、管理用户登陆流程。
在写代码之前,首先需要加入Facebook SDK,在官网下载SDK后,将FacebookSDK.framework加入到工程中,这样我们就可以使用facebook的接口了。
FacebookSDK需要依赖Info.plist中的两个值,分别是FacebookAppID和URL types,如下图所示:
在完成上述工作之后,请求认证就很简单了,代码如下:
void FacebookProxy::login() { if (!FBSession.activeSession.isOpen) { // if the session is closed, then we open it here [FBSession.activeSession openWithCompletionHandler:^( FBSession *session, FBSessionState state, NSError *error) { switch (state) { case FBSessionStateClosedLoginFailed: //TODO handle here. m_isLogined = false; break; default: m_isLogined = true; break; } }]; } }
Facebook的登陆代码是异步回调的,采用匿名函数的方式处理回调也非常方便。
登出的代码如下:
if (m_isLogined){ [FBSession.activeSession closeAndClearTokenInformation]; }
需要注意的是,玩家应用认证之后,由于是采用浏览器(safari)登陆,会在浏览器缓存中留下access_token,如果要切换用户,需要在浏览器中退出账号,然后再登陆。
通过第一步认证,已经可以调用facebook接口了,在本文中,主要介绍获取用户信息和好友信息的接口。
获取用户信息的代码如下所示:void FacebookProxy::loadUserInfo(PlatformUserInfo & info) { [[FBRequest requestForMe] startWithCompletionHandler: ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) { if (!error) { //TODO } }]; }
函数startWithCompletionHandler与认证函数openWithCompletionHandler类似,回调函数的参数user包含了用户的所有信息(包括用户id和用户名等)。
void FacebookProxy::loadFriends(std::vector<PlatformUserInfo*> & friends) { FBRequest* friendsRequest = [FBRequest requestForMyFriends]; [friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection, NSDictionary* result, NSError *error) { NSArray* friends = [result objectForKey:@"data"]; NSLog(@"Found: %i friends", friends.count); for (NSDictionary<FBGraphUser>* friend_ in friends) { NSLog(@"I have a friend named %@ with id %@", friend_.name, friend_.id); } }]; }
void _share(const char * name,const char * caption, const char * desc, const char * link, const char * picture) { NSMutableDictionary * postParams = [[NSMutableDictionary alloc] initWithObjectsAndKeys: [NSString stringWithUTF8String:link], @"link", [NSString stringWithUTF8String:picture], @"picture", [NSString stringWithUTF8String:name], @"name", [NSString stringWithUTF8String:caption], @"caption", [NSString stringWithUTF8String:desc], @"description", nil]; [FBRequestConnection startWithGraphPath:@"me/feed" parameters:postParams HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) { NSString *alertText; if (error) { alertText = [NSString stringWithFormat: @"error: domain = %@, code = %d", error.domain, error.code]; } else { alertText = [NSString stringWithFormat: @"Posted action, id: %@", [result objectForKey:@"id"]]; } // Show the result in an alert [[[UIAlertView alloc] initWithTitle:@"Result" message:alertText delegate:nil cancelButtonTitle:@"OK!" otherButtonTitles:nil] show]; }]; } void FacebookProxy::share(const char * name,const char * caption, const char * desc, const char * link, const char * picture) { if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound) { //No permissions found in session, ask for it [FBSession.activeSession reauthorizeWithPublishPermissions: [NSArray arrayWithObject:@"publish_actions"] defaultAudience:FBSessionDefaultAudienceFriends completionHandler:^(FBSession *session, NSError *error) { if (!error) { // If permissions granted, publish the story _share(name, caption, desc, link, picture); } }]; } else { // If permissions present, publish the story _share(name, caption, desc, link, picture); } }
本文提供的几个接口基本能够满足一般需求,代码仅供参考。