前言
最近在開發(fā)一款意大利的APP中,需要用到FaceBook登錄,記錄下流程。
1. 申請FaceBook賬號、創(chuàng)建應用、獲取應用編號、填寫應用信息,提交審核
2. Xcode 配置信息
3. OC 代碼集成
4. Swift 代碼集成
一、申請FaceBook賬號、創(chuàng)建應用、獲取應用編號、填寫應用信息,提交審核
FaceBook開放平臺地址 : https://developers.facebook.com/docs/facebook-login/ios
查看FaceBook的開發(fā)文檔或者APP測試FaceBook登錄,都需要翻墻。
免費的可以使試試 佛跳墻 。
收費的 同學給推薦了一個socketpro。申請FaceBook賬號流程此處省略。
3.創(chuàng)建應用, 添加登錄功能,設置iOS 配置信息,填寫應用信息,提交審核。

創(chuàng)建應用

選擇用途

填寫應用名稱

3次安全驗證,驗證完后就創(chuàng)建完了

設置登錄

選擇iOS

選擇集成方式,我選擇的是cocoapods

添加bundleID

設置單點登錄

配置info.plist,這個地方寫的有點亂,可以先跳過,直接保存

剩下的步驟直接點繼續(xù)
提交審核的流程,可以參考 審核流程
二、Xcode 配置信息
提交審核通過后,才可以跳轉成功Facebook頁面,或者在應用的設置頁面 添加開發(fā)者人員和測試人員

image.png
- cocoapods 集成導入SDK
#facebook
pod 'FBSDKLoginKit'
3.配置URL types 和 info.plist

配置URL types :fb + 應用編號
右鍵點擊 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.登錄頁面 自定義按鈕的 點擊方法中 實現(xiàn)回調 獲取
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] )
}
- 發(fā)起登錄 - Facebook 原生按鈕
創(chuàng)建FBLoginButton 對象,添加到view中
let loginButton = FBLoginButton()
loginButton.center = view.center view.addSubview(loginButton)
loginButton.delegate = self
view.addSubview(loginButton)
遵循LoginButtonDelegate 并實現(xiàn)代理方法
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
}