使用遊戲中心進行驗證

您可以使用 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. -ObjC 標記新增至目標建構設定的「Other Linker Flags」部分。
  6. 完成後,Xcode 會自動開始在背景中解析並下載依附元件。

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

  1. 請務必向 Firebase 註冊 Apple 應用程式。也就是說,您必須在註冊專區中輸入應用程式的套件 ID,以及其他選填資訊,例如 App Store ID 和團隊 ID 等。這樣才能在完成登入前,安全地驗證使用者 Game Center 憑證的目標對象。
  2. 為 Firebase 專案啟用 Game Center 做為登入供應商:
    1. Firebase 控制台中,開啟「驗證」部分。
    2. 在「Sign in method」分頁中,啟用「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;
}