iOS 第三方登錄 之 FaceBook 登錄(OC + Swift)

前言

最近在開發(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

  1. 查看FaceBook的開發(fā)文檔或者APP測試FaceBook登錄,都需要翻墻。
    免費的可以使試試 佛跳墻 。
    收費的 同學給推薦了一個socketpro

  2. 申請FaceBook賬號流程此處省略。

3.創(chuàng)建應用, 添加登錄功能,設置iOS 配置信息,填寫應用信息,提交審核。


創(chuàng)建應用
選擇用途
填寫應用名稱
3次安全驗證,驗證完后就創(chuàng)建完了
設置登錄
選擇iOS
選擇集成方式,我選擇的是cocoapods
添加bundleID
設置單點登錄
配置info.plist,這個地方寫的有點亂,可以先跳過,直接保存
剩下的步驟直接點繼續(xù)

提交審核的流程,可以參考 審核流程

二、Xcode 配置信息

提交審核通過后,才可以跳轉成功Facebook頁面,或者在應用的設置頁面 添加開發(fā)者人員和測試人員


image.png
  1. 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 代碼集成

  1. 導入頭文件
#import <FBSDKLoginKit/FBSDKLoginKit.h>
  1. 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 代碼集成

  1. 導入頭文件
import FBSDKCoreKit
  1. 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])
    }
  1. 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] )
    }
  1. 發(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

    }
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 今天感恩節(jié)哎,感謝一直在我身邊的親朋好友。感恩相遇!感恩不離不棄。 中午開了第一次的黨會,身份的轉變要...
    余生動聽閱讀 10,914評論 0 11
  • 彩排完,天已黑
    劉凱書法閱讀 4,497評論 1 3
  • 表情是什么,我認為表情就是表現(xiàn)出來的情緒。表情可以傳達很多信息。高興了當然就笑了,難過就哭了。兩者是相互影響密不可...
    Persistenc_6aea閱讀 129,895評論 2 7

友情鏈接更多精彩內容