使用遊戲中心進行驗證

您可以使用 Game Center,讓玩家登入以 Firebase 建構的 Apple 平台遊戲。如要搭配 Firebase 使用 Game Center 登入功能,請先確認本機玩家已登入 Game Center,然後使用 GameCenterAuthProvider 物件產生 Firebase 憑證,以便向 Firebase 驗證。

事前準備

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

  1. 在 Xcode 中保持開啟應用程式專案,然後依序點選「File」(檔案) 和「Add Packages」(新增 Package)。
  2. 系統提示時,請新增 Firebase Apple 平台 SDK 存放區:
  3.   https://github.com/firebase/firebase-ios-sdk.git
  4. 選擇 Firebase Authentication 程式庫。
  5. 在目標建構設定的「Other Linker Flags」部分,新增 -ObjC 標記。
  6. 完成後,Xcode 會自動開始在背景解析及下載依附元件。

接著,請執行下列設定步驟:

  1. 請務必向 Firebase 註冊 Apple 應用程式。也就是說,您必須在註冊專區輸入應用程式的軟體包 ID,以及其他選填資訊,例如應用程式商店 ID 和團隊 ID 等。系統必須先安全地驗證使用者 Game Center 憑證的目標對象,才能完成登入程序。
  2. 為 Firebase 專案啟用 Game Center 做為登入供應商:
    1. 在 Firebase 控制台中,依序前往 「Security」(安全性) >「Authentication」(驗證)。
    2. 在「登入方式」分頁中,啟用 Game Center 登入供應商。

在遊戲中整合 Game Center 登入功能

首先,如果遊戲尚未採用 Game Center,請按照 Apple 開發人員網站上的「Incorporating Game Center into Your Game」和「Authenticating a Local Player on the Device」操作說明進行。

請務必確認提供給 iTunes Connect 的軟體包 ID,與您將應用程式連結至 Firebase 專案時使用的軟體包 ID 相符。

在整合 Game Center 時,您需要定義驗證處理常式,以便在 Game Center 驗證程序中的多個時間點呼叫。在這個處理常式中,檢查玩家是否已登入 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 驗證

確認本機玩家已登入 Game Center 後,請使用 GameCenterAuthProvider.getCredential() 建立 AuthCredential 物件,並將該物件傳遞至 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.
    }];
  }
}];

後續步驟

使用者首次登入後,系統會建立新的使用者帳戶,並連結至其 Game Center ID。這個新帳戶會儲存在 Firebase 專案中,可用於識別專案中所有應用程式的使用者。

在遊戲中,您可以從 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,並用來控管使用者可存取的資料。

如要取得使用者的 Game Center 玩家資訊或存取 Game Center 服務,請使用 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;
}