使用遊戲中心進行驗證

您可以使用 Game Center,讓玩家登入使用 Firebase 打造的 Apple 平台遊戲。目的地: 透過 Firebase 使用遊戲中心登入功能,請先確認本機玩家 登入遊戲中心,然後使用 GameCenterAuthProvider 物件 產生 Firebase 憑證,以便透過 Firebase 進行驗證。

事前準備

使用 Swift Package Manager 安裝及管理 Firebase 依附元件。

  1. 在 Xcode 中保持開啟應用程式專案,然後依序選擇 [檔案] >新增套件
  2. 在系統提示時,新增 Firebase Apple 平台 SDK 存放區:
  3.   https://github.com/firebase/firebase-ios-sdk.git
  4. 選擇 Firebase 驗證資料庫。
  5. 在目標建構設定的「Other Linker Flags」部分中新增 -ObjC 標記。
  6. 完成後,Xcode 會自動開始解析並下載 複製到背景依附元件

接下來,請執行一些設定步驟:

  1. 確認您已向 Firebase 註冊 Apple 應用程式。也就是說,您必須輸入 註冊部分的應用程式軟體包 ID,以及額外的選用選項 例如 App Store ID 和團隊 ID 等。 透過安全的方式驗證使用者的 Game Center 憑證目標對象 登入。
  2. 啟用 Game Center 做為 Firebase 專案的登入提供者:
    1. Firebase 控制台開啟「驗證」專區。
    2. 在「Sign in method」分頁中,啟用「Game Center」 登入資訊

將 Game Center 登入功能整合至遊戲

首先,如果您的遊戲尚未使用 Game Center,請按照 將遊戲中心整合到您的遊戲中, 透過 Apple 裝置在裝置上驗證本機播放器 開發人員網站。

請確認您提供給 iTunes Connect 的軟體包 ID 與您在 用於將應用程式連結至 Firebase 專案時使用的名稱

整合遊戲中心的過程中,您必須定義驗證處理常式 並在 Game Center 驗證程序中的多個時間點呼叫。於 此處理常式,檢查玩家是否已登入遊戲中心。如果是,請 繼續登入 Firebase。

Swift

let localPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler = { (gcAuthViewController?, error) in
  if let gcAuthViewController = gcAuthViewController {
    // Pause any activities that require user interaction, then present the
    // gcAuthViewController to the player.
  } else if localPlayer.isAuthenticated {
    // Player is signed in to Game Center. Get Firebase credentials from the
    // player's Game Center credentials (see below).
  } else {
    // Error
  }
}

Objective-C

__weak GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController *gcAuthViewController,
                                    NSError *error) {
  if (gcAuthViewController != nil) {
    // Pause any activities that require user interaction, then present the
    // gcAuthViewController to the player.
  } else if (localPlayer.isAuthenticated) {
    // Player is signed in to Game Center. Get Firebase credentials from the
    // player's Game Center credentials (see below).
  } else {
    // Error
  }
};

透過 Firebase 驗證

確認本機玩家已登入遊戲中心後, 透過建立 AuthCredential 物件,讓玩家登入遊戲 GameCenterAuthProvider.getCredential(),並將該物件傳送至 signIn(with:):

Swift

// Get Firebase credentials from the player's Game Center credentials
GameCenterAuthProvider.getCredential() { (credential, error) in
  if let error = error {
    return
  }
  // The credential can be used to sign in, or re-auth, or link or unlink.
  Auth.auth().signIn(with:credential) { (user, error) in
    if let error = error {
      return
    }
    // Player is signed in!
  }

Objective-C

// Get Firebase credentials from the player's Game Center credentials
[FIRGameCenterAuthProvider getCredentialWithCompletion:^(FIRAuthCredential *credential,
                                                         NSError *error) {
  // The credential can be used to sign in, or re-auth, or link or unlink.
  if (error == nil) {
    [[FIRAuth auth] signInWithCredential:credential
                              completion:^(FIRUser *user, NSError *error) {
      // If error is nil, player is signed in.
    }];
  }
}];

後續步驟

使用者首次登入後,系統會建立新的使用者帳戶 連結遊戲。這個新帳戶會儲存在您的 Firebase 專案的專用 ID,可用來識別應用程式內所有應用程式的使用者 專案。

您可以在遊戲中透過 User 物件取得使用者的 Firebase UID:

Swift

let user = Auth.auth().currentUser
if let user = user {
  let playerName = user.displayName

  // The user's ID, unique to the Firebase project.
  // Do NOT use this value to authenticate with your backend server,
  // if you have one. Use getToken(with:) instead.
  let uid = user.uid
}

Objective-C

FIRUser *user = [FIRAuth auth].currentUser;
if (user) {
  NSString *playerName = user.displayName;

  // The user's ID, unique to the Firebase project.
  // Do NOT use this value to authenticate with your backend server,
  // if you have one. Use getTokenWithCompletion:completion: instead.
  NSString *uid = user.uid;
}

在 Firebase 即時資料庫和 Cloud Storage 安全性規則中,您可以 已登入使用者在 auth 變數中的不重複使用者 ID,並使用該 ID 來 控制使用者可以存取哪些資料

取得使用者的遊戲中心玩家資訊或存取遊戲中心 服務,請使用 Game Kit 提供的 API。

如要將使用者登出 Firebase,請呼叫 Auth.signOut()

Swift

let firebaseAuth = Auth.auth()
do {
  try firebaseAuth.signOut()
} catch let signOutError as NSError {
  print ("Error signing out: %@", signOutError)
}

Objective-C

NSError *signOutError;
BOOL status = [[FIRAuth auth] signOut:&signOutError];
if (!status) {
  NSLog(@"Error signing out: %@", signOutError);
  return;
}