คุณสามารถใช้ Game Center เพื่อลงชื่อผู้เล่นเข้าสู่เกมแพลตฟอร์ม Apple ที่สร้างบน Firebase หากต้องการใช้การลงชื่อเข้าใช้ Game Center ด้วย Firebase ก่อนอื่นให้ตรวจสอบว่าผู้เล่นในเครื่องลงชื่อเข้าใช้ด้วย Game Center จากนั้นใช้ออบเจ็กต์ GameCenterAuthProvider
เพื่อสร้างข้อมูลรับรอง Firebase ซึ่งคุณสามารถใช้ตรวจสอบสิทธิ์กับ Firebase ได้
ก่อนที่คุณจะเริ่มต้น
ใช้ Swift Package Manager เพื่อติดตั้งและจัดการการอ้างอิง Firebase
- ใน Xcode เมื่อโปรเจ็กต์แอปของคุณเปิดอยู่ ให้ไปที่ File > Add Packages
- เมื่อได้รับแจ้ง ให้เพิ่มที่เก็บ Firebase Apple platforms SDK:
- เลือกไลบรารีการรับรองความถูกต้องของ Firebase
- เมื่อเสร็จแล้ว Xcode จะเริ่มแก้ไขและดาวน์โหลดการอ้างอิงของคุณโดยอัตโนมัติในเบื้องหลัง
https://github.com/firebase/firebase-ios-sdk
ถัดไป ดำเนินการตามขั้นตอนการกำหนดค่า:
- ตรวจสอบว่าคุณลงทะเบียนแอป Apple กับ Firebase ซึ่งหมายถึงการป้อน ID บันเดิลของแอพของคุณในส่วนการลงทะเบียนพร้อมกับข้อมูลทางเลือกเพิ่มเติม เช่น App Store ID และ Team ID เป็นต้น สิ่งนี้จำเป็นสำหรับการยืนยันผู้ชมด้วยข้อมูลรับรอง Game Center ของผู้ใช้อย่างปลอดภัยก่อนที่จะทำการลงชื่อเข้าใช้
- เปิดใช้งาน Game Center เป็นผู้ให้บริการลงชื่อเข้าใช้สำหรับโครงการ Firebase ของคุณ:
- ใน คอนโซล Firebase ให้เปิดส่วน การรับรองความถูกต้อง
- บนแท็บ วิธีการลงชื่อเข้า ใช้ ให้เปิดใช้งานผู้ให้บริการลงชื่อเข้าใช้ Game Center
รวมการลงชื่อเข้าใช้ Game Center เข้ากับเกมของคุณ
ขั้นแรก หากเกมของคุณยังไม่ได้ใช้ Game Center ให้ทำตามคำแนะนำใน การรวม Game Center เข้ากับเกมของคุณ และ รับรองความถูกต้องของผู้เล่นในเครื่องบนอุปกรณ์ บนไซต์นักพัฒนาของ Apple
ตรวจสอบให้แน่ใจว่ารหัสบันเดิลที่คุณให้กับ iTunes Connect นั้นตรงกับรหัสบันเดิลที่คุณใช้เมื่อเชื่อมต่อแอปกับโปรเจ็กต์ Firebase
ในฐานะที่เป็นส่วนหนึ่งของการรวม 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 } }
วัตถุประสงค์-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:)
:
สวิฟต์
// 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! }
วัตถุประสงค์-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 Game Center ของพวกเขา บัญชีใหม่นี้จัดเก็บเป็นส่วนหนึ่งของโปรเจ็กต์ Firebase และใช้เพื่อระบุผู้ใช้ในทุกแอปในโครงการได้
ในเกมของคุณ คุณสามารถรับ Firebase UID ของผู้ใช้ได้จากวัตถุ User
:
สวิฟต์
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 }
วัตถุประสงค์-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 Realtime Database และ Cloud Storage คุณสามารถรับ ID ผู้ใช้ที่ไม่ซ้ำกันของผู้ใช้ที่ลงชื่อเข้าใช้จากตัวแปร auth
และใช้เพื่อควบคุมข้อมูลที่ผู้ใช้สามารถเข้าถึงได้
หากต้องการรับข้อมูลผู้เล่น Game Center ของผู้ใช้หรือเข้าถึงบริการ Game Center ให้ใช้ API ที่ Game Kit ให้ไว้
หากต้องการลงชื่อผู้ใช้ออกจาก Firebase ให้โทร Auth.signOut()
:
สวิฟต์
let firebaseAuth = Auth.auth() do { try firebaseAuth.signOut() } catch let signOutError as NSError { print ("Error signing out: %@", signOutError) }
วัตถุประสงค์-C
NSError *signOutError; BOOL status = [[FIRAuth auth] signOut:&signOutError]; if (!status) { NSLog(@"Error signing out: %@", signOutError); return; }