iOS对接Facebook:登录,分享,邀请,游戏排行榜功能
来源:互联网 发布:volte网络分为终端 编辑:程序博客网 时间:2024/05/18 03:54
在如今的互联网环境下面,如果App仅仅依靠单一的渠道去获取用户量,这是一种很不明智的行为,为了能够更好地推广和使用自己的产品,很多App厂商都会去寻求类似新浪,腾讯,推特以及我们今天所要讲的Facebook这些互联网大佬合作。依托这些有着巨大用户量的平台,帮助提升应用的安装量,及推广有着重要的意义。
在我眼里,使用Facebook绝壁的要比使用微博啊QQ啊高大洋气上档次,可惜遗憾的是在中国要通过翻墙才能使用,不过这并不能阻止中国网民们的热情啊!我一般是使用”云梯VPN”进行翻墙的,觉得还好,包年的话是240元RMB,可以几个人一起合伙买挺划算的 。
好了废话就不多说了,由于公司的项目要拓展到海外市场,所以Facebook渠道成为了对接的首选,今天我就在这边写一个教程关于iOS对接Facebook的相关功能,让大家避免对接时会遇到的一些坑。
在Facebook中添加您的应用
1.登录连接:https://developers.facebook.com/apps
2.点击创建新的App按钮后,出现如下图:
3.填入相应的信息后,点击“创建应用编号”,这样你就会得到你对应的fb-appID.
不得不说,FB对于每一个应用的数据采集做的很人性化,管理者通过后台就可以访问到相应的数据,我截几张图片看下:
环境配置
首先,我们要去Facebook的开发者官网上去下载SDK:SDK下载链接 因为Facebook在中国被墙,所以需要通过翻墙软件才能进行下载。下载好之后它的SDK包含以下几个framework: FBSDKCoreKit.framework、FBSDKLoginKit.framework 和 FBSDKShareKit.framework。
新建一个Xcode工程,取名FBTestDemo(名字随便取),然后将我们下载好的SDK加入到我们的测试项目工程中去,在显示的对话框中,选择 Create groups for any added folders(为添加的文件夹创建组),取消选择 Copy items into destination group’s folder(将项目复制到目标组的文件夹)。这将在 SDK 的安装位置对其进行引用,而不是将 SDK 复制到应用程序中。
Info.Plist配置,参考如下配置:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict> <key>CFBundleDevelopmentRegion</key> <string>en</string> <key>CFBundleExecutable</key> <string>$(EXECUTABLE_NAME)</string> <key>CFBundleIdentifier</key> <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> <key>CFBundleInfoDictionaryVersion</key> <string>6.0</string> <key>CFBundleName</key> <string>$(PRODUCT_NAME)</string> <key>CFBundlePackageType</key> <string>APPL</string> <key>CFBundleShortVersionString</key> <string>1.0</string> <key>CFBundleSignature</key> <string>????</string> <key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array> <string>fb559497127591188</string> </array> </dict> </array> <key>CFBundleVersion</key> <string>1</string> <key>FacebookAppID</key> <string>559497127591988</string> <key>FacebookDisplayName</key> <string>测试</string> <key>LSApplicationQueriesSchemes</key> <array> <string>fbapi</string> <string>fb-messenger-api</string> <string>fbauth2</string> <string>fbshareextension</string> </array> <key>LSRequiresIPhoneOS</key> <true/> <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> <key>NSExceptionDomains</key> <dict> <key>akamaihd.net</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> <false/> </dict> <key>facebook.com</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> <false/> </dict> <key>fbcdn.net</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> <false/> </dict> </dict> </dict> <key>UIMainStoryboardFile</key> <string>Main</string> <key>UIRequiredDeviceCapabilities</key> <array> <string>armv7</string> </array> <key>UISupportedInterfaceOrientations</key> <array> <string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string> </array> <key>UISupportedInterfaceOrientations~ipad</key> <array> <string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationPortraitUpsideDown</string> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string> </array></dict></plist>
委托设置,请将以下代码添加到 AppDelegate.m 文件:
#import <FBSDKCoreKit/FBSDKCoreKit.h>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions]; // 在此添加任意自定义逻辑。 return YES;}- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation ]; // 在此添加任意自定义逻辑。 return handled;}
iOS 版 Facebook 登录
iOS 版 Facebook SDK 让用户可以通过 Facebook 登录注册您的应用。通过 Facebook 登录您的应用时,用户可以向应用授予权限,以便您可以检索信息或以用户的身份在 Facebook 执行操作。
添加自定义登录按钮:
// Add a custom login button to your app UIButton *myLoginButton=[UIButton buttonWithType:UIButtonTypeCustom]; myLoginButton.backgroundColor=[UIColor darkGrayColor]; myLoginButton.frame=CGRectMake(0,0,180,40); myLoginButton.center = self.view.center; [myLoginButton setTitle: @"My Login Button" forState: UIControlStateNormal]; // Handle clicks on the button [myLoginButton addTarget:self action:@selector(loginButtonClicked) forControlEvents:UIControlEventTouchUpInside]; // Add the button to the view [self.view addSubview:myLoginButton];
事件响应函数代码:
// Once the button is clicked, show the login dialog-(void)loginButtonClicked{ FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init]; [login logInWithReadPermissions: @[@"public_profile", @"email", @"user_friends"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) { if (error) { NSLog(@"Process error"); } else if (result.isCancelled) { NSLog(@"Cancelled"); } else { NSLog(@"Logged in"); } }];}
如果应用需要请求除 public_profile、email 和 user_friends 以外的权限,则必须先通过 Facebook 的审核才能发布,例如:publish_actions权限,不可以和上面三个权限放在登录功能一起进行请求,不然App会Crash,所以要获取publish_actions首先要通过登录审核才能请求该权限,请求publish_actions权限代码如下:
static FBSDKLoginManager *loginManager; NSArray *permissions = [[NSArray alloc] initWithObjects: @"publish_actions", nil]; if (!loginManager) { loginManager = [[FBSDKLoginManager alloc] init]; } [loginManager logInWithPublishPermissions:permissions fromViewController:_controller handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) { if ([[FBSDKAccessToken currentAccessToken] hasGranted:@"publish_actions"]){ NSLog(@"publish_actions 已经获取"); } else { NSLog(@"获取 publish_actions 权限"); } }];
iOS 平台Facebook分享
接下来和大家介绍一下如何通过 iOS 应用将内容分享到 Facebook。
链接:
用户通过您的应用将链接分享到 Facebook 时,会包括在帖子中显示的属性:
1.contentURL:要分享的链接
2.contentTitle:表示链接中的内容的标题
3.imageURL:在帖子中显示的缩略图的网址
4.contentDescription:内容的描述,通常为 2-4 个句子
//分享链接- (void)shareLinkContent{ FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init]; content.contentDescription = @"狗粮吃的好饱"; content.contentTitle = @"我就看看不说话"; content.imageURL = [NSURL URLWithString:@"http://avatar.csdn.net/C/5/9/2_shenjie12345678.jpg"]; content.contentURL = [NSURL URLWithString:@"www.baidu.com"]; [FBSDKShareDialog showFromViewController:self withContent:content delegate:nil];}
照片:
用户可以使用分享对话框或自定义界面,通过您的应用将照片分享到 Facebook:
1.照片大小必须小于 12MB
2.用户需要安装版本 7.0 或以上的原生 iOS 版 Facebook 应用
//分享照片 1234为照片的名称- (void)sharePhoto{ FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init]; content.photos = [NSArray arrayWithObject:[FBSDKSharePhoto photoWithImage:[UIImage imageNamed:@"1234"] userGenerated:NO]]; [FBSDKShareDialog showFromViewController:self withContent:content delegate:nil];}
视频:
应用用户可通过分享对话框或您专属的自定义界面将视频分享到 Facebook:
1.视频大小必须小于 12MB。
2.分享内容的用户应安装版本 26.0 或以上的 iOS 版 Facebook 客户端。
//分享视频- (void)shareVedio{ FBSDKShareVideoContent *content = [[FBSDKShareVideoContent alloc] init]; content.video = [FBSDKShareVideo videoWithVideoURL:[NSURL URLWithString:@""] previewPhoto:[FBSDKSharePhoto photoWithImage:[UIImage imageNamed:@"1234"] userGenerated:NO]]; [FBSDKShareDialog showFromViewController:self withContent:content delegate:nil];}
应用邀请
应用邀请:是让用户邀请 Facebook 好友使用移动应用的一种内容丰富的个性化方式。
应用邀请共有 2 个参数会传送到邀请对话框:应用链接网址和预览图片网址。
应用链接网址:接收人点击应用邀请页面的 install/play(安装/试玩)按钮时,所打开的应用链接(必须有)。
预览图片网址:邀请中所用图片的网址(可不传)。
预览图片网址用于在邀请中呈现图片。虽然预览图片网址并非必需的参数,但还是建议传送,因为如果系统找不到替代图片,您的邀请将可能无法呈现。
建议的图片尺寸为 1,200 x 628 像素,高宽比 1.9:1。
代码如下:
-(void)inviteFriends:(NSString *)appUrl imageURL:(NSString *)imageUrl baseViewController:(UIViewController *)baseViewController{ FBSDKAppInviteContent *content =[[FBSDKAppInviteContent alloc] init]; content.appLinkURL = [NSURL URLWithString:appUrl]; //optionally set previewImageURL content.appInvitePreviewImageURL = [NSURL URLWithString:imageUrl]; // present the dialog. Assumes self implements protocol `FBSDKAppInviteDialogDelegate` [FBSDKAppInviteDialog showFromViewController:baseViewController withContent:content delegate:[FBUtil getInstance]];}
游戏排行榜
构建社交类游戏时,比较玩家及其好友的游戏进度是非常普遍的做法。这是促进社交竞争的绝佳方式,且 Facebook 提供了分数 API 和成就 API 等工具来推动这类竞争。
分数 API 和成就 API 让开发者能够轻松构建社交排行榜,以跨平台的社交形式保存玩家的分数以及向玩家颁发成就。通过分数 API,您可以存储和重置玩家的高分,以及拉取玩家好友的分数列表,展示在排行榜内。
排行榜提交分数
/* *@brief facebook 游戏排行榜 上传游戏分数 *@param scores 分数 *@param _block 提交回调 */+ (void)sendFBScoresWithCallback:(int)scores callback:(sendScoresCallBack)_block{ NSString *score = [NSString stringWithFormat:@"%d", scores]; NSDictionary *params = @{@"score": score}; FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"/me/scores" parameters:params HTTPMethod:@"POST"]; [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { // Handle the result if(!error && result){ _block(result); return; }else{ NSLog(@"error:%@", error); } _block(nil); }];}
获取排行榜好友数据
/* *@brief facebook 获取排行榜榜单排名 *@param _block 排行榜信息回调 */+ (void)fetchScoresWithCallback:(fetchLeadBoardCallBack)_block{ FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:[NSString stringWithFormat:@"%@/scores", [FBSDKSettings appID]] parameters:@{@"fields":@"score,user"} HTTPMethod:@"GET"]; [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { // Handle the result if(!error && result){ NSArray* fetchedScoreData = [[NSArray alloc] initWithArray:[result objectForKey:@"data"]]; if ([fetchedScoreData count] > 0){ _block(fetchedScoreData); return; } }else{ NSLog(@"error:%@", error); } _block(nil); }];}
好了大致的内容就以上这些,Facebook在对接上其实么有那么困难,它的文档里面代码也整理的比较清晰,唯一的难点就是它需要翻墙去测试,网络有可能会不太稳定,But, 没关系,凡是多测试几次,一步一步按照上面的说明来配置。
相关文档:
https://developers.facebook.com/docs
https://developers.facebook.com/docs/games/services/scores-achievements
https://developers.facebook.com/docs/facebook-login/ios
https://developers.facebook.com/docs/invite
我会继续更新内容的,谢谢。
欢迎大家关注我的微信公众号,有什么问题可以随时联系,扫描下方二维码添加:
- iOS对接Facebook:登录,分享,邀请,游戏排行榜功能
- Unity 接入Facebook。登录、分享、邀请、好友列表等功能。详细记录
- IOS 自带facebook登录分享
- iOS友盟三方分享登录功能
- Facebook 登录和分享
- Facebook SDK 登录分享
- facebook登录和分享
- 【IOS和Android】Cocos2d-x 集成Facebook分享功能
- IOS项目集成ShareSDK分享登录功能
- IOS,Android-Facebook分享
- iOS 直播登录facebook
- Android Facebook分享功能实现
- android 集成Facebook 分享功能
- 网页游戏与平台登录对接流程
- 用iframe方式在facebook中实现邀请功能
- facebook iOS Tutorial (分享到facebook)
- IOS 植入Facebook SDK --- 分享
- (NO.00001)iOS游戏SpeedBoy Lite成形记(二十八):增加排行榜功能
- HDOJ 2066 一个人的旅行(dijkstra)
- Win10下的Linux子系统之Bash
- 贪心——Windows10 ( HDU 5802 ) ( 2016 Multi-University Training Contest 6 1010 )
- 三元表达式的嵌套
- CentOS/Linux 端口开放关闭管理
- iOS对接Facebook:登录,分享,邀请,游戏排行榜功能
- 关于DefaultHttpClient 过时的问题
- 指定任意灰度变换
- Prism框架(二)——Prism应用程序初始化
- poj 3261 Milk Patterns
- 《组合变身小宠物游戏》GameFSM(修改更新中)【初学者】
- 【转】R语言异常检测处理
- 开篇
- 设计模式之UML类图