Facebook第三方登入
[TOC]
ios 13
xcode 12.2
Cocoapods安裝
首先必須先安裝 cocoapods
1 2 3
| gem install cocoapods
sudo gem install cocoapods
|
初始化Pod
至專案目錄下指令
完成後會再專案根目錄出現Pod的檔案,點開編輯輸入
1 2 3 4 5 6 7 8
| target '你的專案名稱' do
pod 'FacebookCore' pod 'FacebookLogin' pod 'FacebookShare' end
|
之後在終端機專案目錄下執行
完成之後會在專案目錄下出現一個檔名為workspce的檔案,之後要撰寫程式都從此進入
Facebook後台設定
後台設定可以參考官方文件: 官方連結
這邊主要是將官方提供的xml輸入到自己的info.plist裡面
程式實作
創建類別
創一個類別叫FacebookLogin並import FBSDKLoginKit
1 2 3 4 5 6
| import UIKit import FBSDKLoginKit
class FacebookLogin: NSObject, LoginButtonDelegate {
}
|
訂類型別名
訂一個類型別名方便閱讀,此程序非必要,依照個人撰寫習慣
1 2 3
| public typealias FBRequestHandler = (_ connection:GraphRequestConnection?, _ result:Any?, _ error:Error?)->()
|
創建設定函式
於FacebookLogin內創建一個在開啟時使執行設定的函式
1 2 3 4 5
| class func setup(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) { ApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions) }
|
呼叫第三方登入
成功則將會回傳使用者資料
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class func login(viewController:UIViewController, completion: @escaping (_ token:String) -> Void) { let fbLoginManager : LoginManager = LoginManager() fbLoginManager.logOut() fbLoginManager.logIn(permissions: ["email"], from: viewController) { (result, error) -> Void in if (error == nil) { if (result?.isCancelled)! { print("login has been canceled") return } else { self.getFBUserData(completion: { (connection, result, error) in if (error == nil) { if let currentToken = AccessToken.current { completion(currentToken.tokenString) } } }) } } } }
|
處理使用者的資料
1 2 3 4 5 6 7 8 9 10 11 12
| class private func getFBUserData(completion:@escaping (FBRequestHandler)) { if ((AccessToken.current) != nil) { GraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in if error == nil { completion(connection, result, error) } else { print(error) completion(connection, nil, error) } }) } }
|
處理APP轉跳
於FacebookLogin內創建一個處理APP轉跳的函式
1 2 3
| class func openURL(_ 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]) }
|
可依照需求處理相對應流程
1 2 3 4 5 6 7 8
| func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?) { } func loginButtonDidLogOut(_ loginButton: FBLoginButton) { }
|
使用方法
在AppDelegate設定
1 2 3 4 5 6 7 8
| func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { FacebookLogin.setup(application, didFinishLaunchingWithOptions: launchOptions)
return true }
|
處理轉跳
1 2 3 4 5 6
| func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { let facebookLogin = FacebookLogin.openURL(app, open: url, options: options)**** return facebookLogin }
|
在需要呼叫第三方登入的viewController呼叫即可
1 2 3
| FacebookLogin.login(viewController: self) { (token) in }
|