使用 Game Center 進行身份驗證

您可以使用 Game Center 讓玩家登入基於 Firebase 建置的 Apple 平台遊戲。若要透過 Firebase 使用 Game Center 登錄,請先確保本機玩家已透過 Game Center 登錄,然後使用GameCenterAuthProvider物件產生 Firebase 憑證,您可以使用該憑證透過 Firebase 進行驗證。

在你開始之前

使用 Swift Package Manager 安裝和管理 Firebase 相依性。

  1. 在 Xcode 中,開啟應用程式項目,導覽至File > Add Packages
  2. 出現提示時,新增 Firebase Apple 平台 SDK 儲存庫:
  3.   https://github.com/firebase/firebase-ios-sdk.git
  4. 選擇 Firebase 身份驗證庫。
  5. -ObjC標誌新增至目標建置設定的「其他連結器標誌」部分。
  6. 完成後,Xcode 將自動開始在背景解析並下載您的依賴項。

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

  1. 確保您使用 Firebase 註冊您的 Apple 應用程式。這意味著在註冊部分輸入應用程式的捆綁包 ID 以及其他可選信息,例如應用程式商店 ID 和團隊 ID 等。在完成登入之前,需要安全地驗證使用者的 Game Center 憑證的受眾。
  2. 啟用 Game Center 作為 Firebase 專案的登入提供者:
    1. Firebase 控制台中,開啟「驗證」部分。
    2. 「登入方法」標籤上,啟用Game Center登入提供者。

將 Game Center 登入整合到您的遊戲中

首先,如果您的遊戲尚未使用 Game Center,請按照 Apple 開發者網站上將Game Center 合併到您的遊戲中在裝置上驗證本機播放器中的說明進行操作。

確保您提供給 iTunes Connect 的捆綁包 ID 與您將應用程式連接到 Firebase 專案時使用的捆綁包 ID 相符。

作為 Game Center 整合的一部分,您可以定義一個身分驗證處理程序,該處理程序會在 Game Center 驗證過程中的多個點呼叫。在此處理程序中,檢查玩家是否已登入 Game Center。如果是這樣,您可以繼續登入 Firebase。

迅速

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 進行身份驗證

在確定本地玩家已登入 Game Center 後,透過使用GameCenterAuthProvider.getCredential()建立AuthCredential物件並將該物件傳遞給signIn(with:)讓玩家登入您的遊戲:

迅速

// 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.
    }];
  }
}];

下一步

當使用者首次登入後,系統會建立一個新使用者帳戶並將其連結到他們的 Game Center ID。這個新帳戶將作為 Firebase 專案的一部分存儲,可用於識別專案中每個應用程式中的使用者。

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

迅速

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 即時資料庫和雲端儲存安全性規則中,您可以從auth變數取得登入使用者的唯一使用者 ID,並使用它來控制使用者可以存取哪些資料。

如需取得使用者的 Game Center 玩家資訊或存取 Game Center 服務,請使用Game Kit提供的 API。

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

迅速

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;
}