אימות באמצעות מרכז המשחקים

אתם יכולים להשתמש ב-Game Center כדי להכניס שחקנים למשחק בפלטפורמות של אפל שמבוסס על Firebase. כדי להשתמש בכניסה ל-Game Center באמצעות Firebase, קודם צריך לוודא שהשחקן המקומי מחובר ל-Game Center, ואז להשתמש באובייקט GameCenterAuthProvider כדי ליצור פרטי כניסה ל-Firebase, שבהם אפשר להשתמש כדי לבצע אימות באמצעות Firebase.

לפני שמתחילים

אתם יכולים להשתמש ב-Swift Package Manager כדי להתקין ולנהל את יחסי התלות של Firebase.

  1. ב-Xcode, כשהפרויקט של האפליקציה פתוח, עוברים אל File > Add Packages (קובץ > הוספת חבילות).
  2. כשמוצגת בקשה, מוסיפים את מאגר ה-SDK של Firebase לפלטפורמות של אפל:
  3.   https://github.com/firebase/firebase-ios-sdk.git
  4. בוחרים בספרייה Firebase Authentication.
  5. מוסיפים את הדגל -ObjC לקטע Other Linker Flags (דגלים אחרים של מקשרים) בהגדרות ה-build של היעד.
  6. אחרי שתסיימו, פלטפורמת Xcode תתחיל לטפל ביחסי התלות ולהוריד אותם ברקע באופן אוטומטי.

עכשיו מבצעים כמה שלבי הגדרה:

  1. חשוב לוודא שהאפליקציה שלכם ל-Apple רשומה ב-Firebase. כלומר, צריך להזין את מזהה החבילה של האפליקציה בקטע הרישום, יחד עם מידע אופציונלי נוסף כמו מזהה App Store ומזהה צוות וכו'. זה נדרש כדי לאמת באופן מאובטח את הקהל של פרטי הכניסה של המשתמש ל-Game Center לפני השלמת הכניסה.
  2. מפעילים את Game Center כספק כניסה לפרויקט Firebase:
    1. במסוף Firebase, נכנסים אל אבטחה > אימות.
    2. בכרטיסייה שיטת התחברות, מפעילים את ספק הכניסה Game Center.

שילוב הכניסה ל-Game Center במשחק

אם המשחק שלכם עדיין לא משתמש ב-Game Center, קודם צריך לפעול לפי ההוראות במאמרים Incorporating Game Center into Your Game ו-Authenticating a Local Player on the Device באתר המפתחים של אפל.

חשוב לוודא שמזהה החבילה שאתם מספקים ל-iTunes Connect תואם למזהה החבילה שבו השתמשתם כשקישרתם את האפליקציה לפרויקט Firebase.

כחלק מהשילוב של Game Center, אתם מגדירים handler לאימות שנקרא בכמה נקודות בתהליך האימות של Game Center. ב-handler הזה, בודקים אם השחקן מחובר באמצעות 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, מכניסים את השחקן למשחק על ידי יצירת אובייקט 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.
    }];
  }
}];

השלבים הבאים

אחרי שמשתמש נכנס בפעם הראשונה, נוצר חשבון משתמש חדש שמקושר למזהה שלו ב-Game Center. החשבון החדש הזה נשמר כחלק מהפרויקט ב-Firebase, ואפשר להשתמש בו כדי לזהות משתמש בכל אפליקציה בפרויקט.

במשחק, אפשר לקבל את מזהה המשתמש ב-Firebase מהאובייקט User:

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, ולהשתמש בו כדי לקבוע לאילו נתונים המשתמש יכול לגשת.

כדי לקבל את פרטי השחקן של המשתמש ב-Game Center או כדי לגשת לשירותי Game Center, משתמשים בממשקי ה-API שסופקו על ידי Game Kit.

כדי להוציא משתמש מ-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;
}