คุณใช้ Game Center เพื่อลงชื่อเข้าใช้ให้ผู้เล่นในเกมบนแพลตฟอร์ม Apple ที่สร้างใน Firebase ได้ หากต้องการใช้การลงชื่อเข้าใช้ Game Center กับ Firebase ก่อนอื่นให้ตรวจสอบว่าผู้เล่นในพื้นที่ลงชื่อเข้าใช้ Game Center แล้ว จากนั้นใช้ออบเจ็กต์ GameCenterAuthProvider เพื่อสร้างข้อมูลเข้าสู่ระบบ Firebase ซึ่งคุณสามารถใช้เพื่อตรวจสอบสิทธิ์กับ Firebase ได้
ก่อนเริ่มต้น
ใช้ Swift Package Manager เพื่อติดตั้งและจัดการทรัพยากร Dependency ของ Firebase
- ใน Xcode ขณะที่โปรเจ็กต์แอปเปิดอยู่ ให้ไปที่ไฟล์ > เพิ่มแพ็กเกจ
- เมื่อได้รับข้อความแจ้ง ให้เพิ่มที่เก็บ SDK ของ Firebase สำหรับแพลตฟอร์ม Apple ดังนี้
- เลือกคลัง Firebase Authentication
- เพิ่มแฟล็ก
-ObjCลงในส่วนแฟล็ก Linker อื่นๆ ของการตั้งค่าบิลด์ของเป้าหมาย - เมื่อเสร็จแล้ว Xcode จะเริ่มแก้ไขและดาวน์โหลดทรัพยากร Dependency ในเบื้องหลังโดยอัตโนมัติ
https://github.com/firebase/firebase-ios-sdk.git
จากนั้นให้ทำตามขั้นตอนการกำหนดค่าต่อไปนี้
- ตรวจสอบว่าคุณได้ลงทะเบียนแอป Apple กับ Firebase แล้ว ซึ่งหมายถึงการป้อนรหัสชุดของแอปในส่วนการลงทะเบียนพร้อมกับข้อมูลเพิ่มเติมที่ไม่บังคับ เช่น รหัส App Store และรหัสทีม เป็นต้น ซึ่งจำเป็นสำหรับการยืนยันกลุ่มเป้าหมายของข้อมูลเข้าสู่ระบบ 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 ต่อไปได้
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 UID ของผู้ใช้จากออบเจ็กต์ 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; }