Możesz używać Game Center do logowania graczy w grze na platformy Apple utworzonej w Firebase. Aby używać logowania przez Game Center w Firebase, najpierw upewnij się, że lokalny gracz jest zalogowany w Game Center, a następnie użyj obiektu GameCenterAuthProvider do wygenerowania danych logowania Firebase, których możesz użyć do uwierzytelniania w Firebase.
Zanim zaczniesz
Do instalowania zależności Firebase i zarządzania nimi używaj menedżera pakietów Swift.
- W Xcode, gdy projekt aplikacji jest otwarty, kliknij File > Add Packages (Plik > Dodaj pakiety).
- Gdy pojawi się prośba, dodaj repozytorium pakietu SDK Firebase na platformy Apple:
- Wybierz bibliotekę Firebase Authentication.
- Dodaj flagę
-ObjCdo sekcji Other Linker Flags (Inne flagi tagu łączącego) w ustawieniach kompilacji celu. - Gdy skończysz, Xcode zacznie automatycznie przetwarzać i pobierać zależności w tle.
https://github.com/firebase/firebase-ios-sdk.git
Następnie wykonaj te czynności konfiguracyjne:
- Pamiętaj, aby zarejestrować aplikację na Apple w Firebase. Oznacza to, że w sekcji rejestracji musisz wpisać identyfikator pakietu aplikacji oraz dodatkowe opcjonalne informacje, takie jak identyfikator sklepu z aplikacjami i identyfikator zespołu. Będzie to wymagane do bezpiecznego zweryfikowania odbiorców danych logowania użytkownika w centrum gier przed zakończeniem logowania.
- Włącz Game Center jako dostawcę logowania w projekcie w Firebase:
- W konsoli Firebase otwórz Zabezpieczenia > Uwierzytelnianie.
- Na karcie Metoda logowania włącz dostawcę logowania Game Center.
Integracja logowania w Centrum gier z grą
Jeśli Twoja gra nie korzysta jeszcze z Game Center, postępuj zgodnie z instrukcjami podanymi w artykułach Incorporating Game Center into Your Game (Włączanie Game Center w grze) i Authenticating a Local Player on the Device (Uwierzytelnianie lokalnego gracza na urządzeniu) na stronie Apple dla deweloperów.
Upewnij się, że identyfikator pakietu podany w iTunes Connect jest zgodny z identyfikator pakietu używanym podczas łączenia aplikacji z projektem w Firebase.
W ramach integracji z Game Center definiujesz moduł obsługi uwierzytelniania, który jest wywoływany w różnych momentach procesu uwierzytelniania Game Center. W tym module obsługi sprawdź, czy gracz jest zalogowany w Game Center. Jeśli tak, możesz kontynuować logowanie się w 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 } };
Uwierzytelnianie za pomocą Firebase
Po ustaleniu, że lokalny gracz zalogował się w Game Center, zaloguj go w grze, tworząc obiekt AuthCredential za pomocą GameCenterAuthProvider.getCredential() i przekazując go do 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. }]; } }];
Dalsze kroki
Gdy użytkownik zaloguje się po raz pierwszy, zostanie utworzone nowe konto użytkownika i połączone z jego identyfikatorem Game Center. To nowe konto jest przechowywane w projekcie w Firebase i może służyć do identyfikowania użytkownika we wszystkich aplikacjach w projekcie.
W grze możesz uzyskać identyfikator Firebase UID użytkownika z obiektu 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; }
W regułach zabezpieczeń Bazy danych czasu rzeczywistego Firebase i Cloud Storage możesz pobrać unikalny identyfikator zalogowanego użytkownika ze zmiennej auth i użyć go do kontrolowania, do jakich danych użytkownik ma dostęp.
Aby uzyskać informacje o graczu w Game Center lub uzyskać dostęp do usług Game Center, użyj interfejsów API udostępnianych przez Game Kit.
Aby wylogować użytkownika z Firebase, wywołaj 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; }