前言
最近在开发一款意大利的APP中,需要用到FaceBook登录,记录下流程。
1. 申请FaceBook账号、创建应用、获取应用编号、填写应用信息,提交审核
2. Xcode 配置信息
3. OC 代码集成
4. Swift 代码集成
一、申请FaceBook账号、创建应用、获取应用编号、填写应用信息,提交审核
FaceBook开放平台地址 : https://developers.facebook.com/docs/facebook-login/ios
3.创建应用, 添加登录功能,设置iOS 配置信息,填写应用信息,提交审核。
提交审核的流程,可以参考 审核流程
二、Xcode 配置信息
提交审核通过后,才可以跳转成功Facebook页面,或者在应用的设置页面 添加开发者人员和测试人员
- cocoapods 集成导入SDK
#facebook
pod 'FBSDKLoginKit'
3.配置URL types 和 info.plist
右键点击 info.plist, 选择 Open As 然后选择Source Code, 粘贴下面的代码,替换自己的应用编号 和应用名称
<key>FacebookAppID</key>
<string>应用编号</string>
<key>FacebookDisplayName</key>
<string>应用名称</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fbapi20130214</string>
<string>fbapi20130410</string>
<string>fbapi20130702</string>
<string>fbapi20131010</string>
<string>fbapi20131219</string>
<string>fbapi20140410</string>
<string>fbapi20140116</string>
<string>fbapi20150313</string>
<string>fbapi20150629</string>
<string>fbapi20160328</string>
<string>fbauth</string>
<string>fb-messenger-share-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
三、OC 代码集成
- 导入头文件
#import <FBSDKLoginKit/FBSDKLoginKit.h>
- AppDelegate.m 下
didFinishLaunchingWithOptions
方法下 初始化SDK
//初始化facebook SDK
[[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(nonnull NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
[[FBSDKApplicationDelegate sharedInstance] application:application openURL:url options:options];
return YES;
}
3.登录页面 自定义按钮的 点击方法中 实现回调 获取
FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
[loginManager logInWithPermissions:@[@"public_profile"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult * _Nullable result, NSError * _Nullable error) {
if (error) {
NSLog(@"Process error");
} else if (result.isCancelled) {
NSLog(@"Cancelled");
} else {
NSString *facebookId = result.token.userID;
//用户的facebookId 传给后台 判断该用户是否绑定手机号,如果绑定了直接登录,如果没绑定跳绑定手机号页面
}
}];
四、Swift 代码集成
- 导入头文件
import FBSDKCoreKit
- AppDelegate.swift
func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool {
ApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func application( _ app:UIApplication, open url:URL, options: [UIApplication.OpenURLOptionsKey :Any] = [:] ) -> Bool {
return ApplicationDelegate.shared.application( app, open: url, sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplication.OpenURLOptionsKey.annotation])
}
- iOS13.0配置 SceneDelegate.swift
func scene(_ scene:UIScene, openURLContexts URLContexts:Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else { return }
ApplicationDelegate.shared.application( UIApplication.shared, open: url, sourceApplication: nil, annotation: [UIApplication.OpenURLOptionsKey.annotation] )
}
- 发起登录 - Facebook 原生按钮
创建FBLoginButton 对象,添加到view中
let loginButton = FBLoginButton()
loginButton.center = view.center view.addSubview(loginButton)
loginButton.delegate = self
view.addSubview(loginButton)
遵循LoginButtonDelegate 并实现代理方法
func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?) {
if result!.isCancelled {
print("取消登录")
} else {
print("loginButton")
}
}
func loginButtonDidLogOut(_ loginButton: FBLoginButton) {
print("loginButtonDidLogOut")
}
自定义按钮
let login = LoginManager.init()
login.logIn(permissions: ["public_profile", "email"], from: self) { (result, error) in
}