最近由于项目需要集成
- 首先到Facebook官网注册一个账号,然后创建一个应用,创建完应用后将APPID保存,以备后面使用;
- 点此下载iOS平台的SDK;
- 集成SDK到项目中,具体操作如下:
1.打开下载的SDK的目录~/Documents/FacebookSDK
;
2.拖拽Bolts.framework
,FBSDKCoreKit.Framework
,
FBSDKLoginKit.Framework
,FBSDKShareKit.Framework
到项目的Frameworks
组中。在拖拽选项中选择Copy items if needed和Create groups
。
- 配置
Xcode
工程,点击info.plist
文件,右键选择Open As Source Code
,将如下代码复制即可:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb+APPID</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>APPID</string>
<key>FacebookDisplayName</key>
<string>APP名称</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
- 添加依赖库,点击
target->General->Linked Frameworks and Libraries
,点击左下角的+号按钮,依次添加以下系统资源库:
Security.framework
libiconv.dylib
SystemConfiguration.framework
CoreGraphics.Framework
libsqlite3.dylib
CoreTelephony.framework
libstdc++.dylib
libz.dylib
Accounts.framework
- 在
AppDelegate
导入import <FBSDKCoreKit/FBSDKCoreKit.h>
,并实现FBSDKApplicationDelegate
代理方法,具体代码如下:
- (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 {
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation
];
}
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
return [[FBSDKApplicationDelegate sharedInstance] application:app openURL:url sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey] annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
}
打开
Keychain
,在Xcode8
和iOS 10
的情况下,还需要做一步操作,那就是打开Keychain
。
在Xcode
左侧导航栏最上面,点击“项目名称”,在右侧面板中选择Capabilities
,在下面找到Keychain Sharing
,点击ON
按钮。测试登录(这里采用的是自定义按钮的方式)
1.在需要登录的Controller中导入如下头文件:
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
2.添加对应的登录代码
FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
[loginManager logOut];// 先退出登录
[loginManager logInWithReadPermissions:@[@"public_profile",@"user_friends"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error == nil) {
//登录成功
NSLog(@"===userID%@====token%@",result.token.userID,result.token.tokenString);
FBSDKLoginManagerLoginResult *response = result;
NSDictionary*params= @{@"fields":@"id,name,email,age_range,first_name,last_name,link,gender,locale,picture,timezone,updated_time,verified"};
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:result.token.userID
parameters:params
HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
NSLog(@"%@",result[@"name"]);
if (error == nil) {
NSDictionary *picture = result[@"picture"];
NSDictionary *data = picture[@"data"];
NSString *avatar = @"";
if (data[@"url"] != nil && ![data[@"url"] isKindOfClass:[NSNull class]]) {
avatar = data[@"url"];
}
NSString *userName = @"";
if (result[@"name"] != nil && ![result[@"name"] isKindOfClass:[NSNull class]]) {
userName = result[@"name"];
}
NSString *unionid = @"";
if (result[@"id"] != nil && ![result[@"id"] isKindOfClass:[NSNull class]]) {
unionid = result[@"id"];
}
}else{
[self.view makeToast:[NSString stringWithFormat:@"登录失败,错误码为%ld",error.code] duration:3 position:CSToastPositionCenter];
}
}];
}else{
//登录失败
[self.view makeToast:[NSString stringWithFormat:@"登录失败,错误码为%ld",error.code] duration:3 position:CSToastPositionCenter];
NSLog(@"----%ld",error.code);
}
}];
这里需要注意的是,官网上写的是通过监听拿到对应的头像和姓名等信息,但是在集成过程中发现拿到的信息是nil,所以采用上述方式获取想要的信息.
- 需要获取的权限
一般情况下,使用第三方登录,只需要
public_profile
,and
user_friends
这三种权限就可以了,此时在app
的设置页需要填写联系邮箱才能拥有这些权限。这种情况下,不需要
如果你的应用需要请求public_profile
,and
user_friends
之外的额外需求,在你发布应用之前,
至此,Facebook
第三方登录就集成完了,如果在集成的过程中有任何问题,欢迎在评论区留言,我会在第一时间给予回复。