使用遊戲中心進行驗證

您可以使用 Game Center,讓玩家登入使用 Firebase 打造的 Apple 平台遊戲。如要透過 Firebase 使用遊戲中心登入功能,請先確認本機玩家已透過 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. 在目標建構設定的「Other Linker Flags」部分中新增 -ObjC 標記。
  6. 完成後,Xcode 會自動開始在背景解析並下載依附元件。

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

  1. 確認您已向 Firebase 註冊 Apple 應用程式。這意味著在註冊部分輸入應用程式的軟體包 ID,以及其他選用資訊,例如 App Store ID 和團隊 ID 等。如要完成登入程序,就必須以安全的方式驗證使用者的遊戲中心憑證目標對象。
  2. 啟用 Game Center 做為 Firebase 專案的登入提供者:
    1. Firebase 控制台開啟「驗證」專區。
    2. 在「Sign in method」分頁中,啟用「Game Center」登入提供者。

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

首先,如果您的遊戲尚未使用 Game Center,請按照 Apple 開發人員網站上「將遊戲中心整合到遊戲」和「在裝置上驗證本機玩家」一文的指示操作。

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

整合遊戲中心時,您可以定義驗證處理常式,該處理常式會在遊戲中心驗證程序中的多個時間點呼叫。在這個處理常式中,檢查玩家是否透過遊戲中心登入。在這種情況下,你可以繼續登入 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 驗證

確認本機玩家已透過遊戲中心登入後,請使用 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.
    }];
  }
}];

後續步驟

使用者首次登入後,系統會建立新的使用者帳戶,並連結至他們的遊戲中心 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,並使用該 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;
}