iT邦幫忙

2021 iThome 鐵人賽

DAY 19
0
Mobile Development

在 iOS 開發路上的大小事系列 第 19

【在 iOS 開發路上的大小事-Day19】透過 Firebase 來管理使用者 (Sign in with Facebook 篇) Part1

前置作業

在 Podfile 裡面新增 Firebase/Auth、FBSDKLoginKit 這兩個套件

pod 'Firebase/Auth'
pod 'FBSDKLoginKit'

pod install


接著到 Facebook Developers 網站 註冊成為開發人員
註冊完成後,會看到下面這個畫面

接著點中間的「建立應用程式」,選擇你要建立的應用程式類型,如果不知道要選哪一種類型的話,可以選最後一個「無」,因為擁有最多的權限及功能
這邊由於只需要用到 Facebook 登入功能,不會用到企業商家的功能,所以選「消費者」就可以了

填入應用程式顯示名稱、聯絡電子郵件,都填好就按建立應用程式

這樣就算建立完成了,這樣就可以看到這個程式的 Facebook Developers 後台了

再到 iOS 專用 Facebook 登入 - 快速入門 這個網站
專案的 Bundle ID啟用應用程式的單一登入 這兩個選項填好

然後就照著說明操作就可以了

應用程式編號如下圖位置

Client Token 如下圖位置

打開 App Target → Info,新增一個 URL Types 欄位,然後照著圖上說明填上對應的資料

URL Schemes 欄位範例:
假設你的應用程式編號是 123456789012345,欄位內就填「fb123456789012345」
假設你的應用程式編號是 123456789098765,欄位內就填「fb123456789098765」

然後再右鍵以 Source Code 開啟 Info.plist,在最後一個「」上面填入下面的資料

<key>FacebookAppID</key>
<string>你的應用程式編號</string>
<key>FacebookClientToken</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>

填完後會長得像下面一樣 ↓

如果 App 的系統安裝最低版本是 iOS 12 的話,則是加入下面的程式碼

// AppDelegate.swift
import UIKit
import FBSDKCoreKit
@UIApplicationMain
class AppDelegate:UIResponder, UIApplicationDelegate { 
    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 {
        ApplicationDelegate.shared.application(app, open: url, sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplication.OpenURLOptionsKey.annotation])
    } 
}

// SceneDelegate.swift
@available(iOS 13.0, *)
import FBSDKCoreKit
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])
} 

如果 App 的系統安裝最低版本是 iOS 13 的話,則是加入下面的程式碼

// AppDelegate.swift
import FBSDKCoreKit
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    ApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
    return true
}

// SceneDelegate.swift
import FBSDKCoreKit
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])
} 

再來到 Firebase Console → Authentication,將 Facebook 登入開啟

這樣就完成前置作業了,明天再來進行實作~

本篇的範例程式碼:

  1. AppDelegate.swift:Github
  2. SceneDelegate.swift:Github

上一篇
【在 iOS 開發路上的大小事-Day18】透過 Firebase 來管理使用者 (Sign in with Google 篇) Part2
下一篇
【在 iOS 開發路上的大小事-Day20】透過 Firebase 來管理使用者 (Sign in with Facebook 篇) Part2
系列文
在 iOS 開發路上的大小事30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言