Skip to content

Add support of Game Center sign in #2127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 127 additions & 2 deletions Example/Auth/Sample/MainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#import "MainViewController.h"

#import <objc/runtime.h>
#import <GameKit/GameKit.h>

#import <FirebaseCore/FIRApp.h>
#import <FirebaseCore/FIRAppInternal.h>
Expand All @@ -25,6 +26,7 @@
#import "AppManager.h"
#import "AuthCredentials.h"
#import "FIRAdditionalUserInfo.h"
#import "FIRGameCenterAuthProvider.h"
#import "FIROAuthProvider.h"
#import "FIRPhoneAuthCredential.h"
#import "FIRPhoneAuthProvider.h"
Expand Down Expand Up @@ -589,11 +591,41 @@
static NSString *const kPhoneNumberSignInReCaptchaTitle = @"Sign in With Phone Number";

/** @var kVerifyIOSClientTitle
@brief The title for button to verify iOS client.
@brief The title for button to verify iOS client.
*/
static NSString *const kVerifyIOSClientTitle = @"Verify iOS client";

/** @var kIsNewUserToggleTitle
/** @var kGameCenterAuthSectionTitle
@brief The title for the section of Game Center
*/
static NSString *const kGameCenterAuthSectionTitle = @"Game Center";

/** @var kLogInWithSystemGameCenterTitle
@brief The title for the button to log into the Game Center account
*/
static NSString *const kLogInWithSystemGameCenterTitle = @"Log In System Game Center";

/** @var kSignInWithGameCenterTitle
@brief The title for the button to sign in with Game Center
*/
static NSString *const kSignInWithGameCenterTitle = @"Sign in Game Center";

/** @var kLinkWithGameCenterTitle
@brief The title for the button to link with Game Center
*/
static NSString *const kLinkWithGameCenterTitle = @"Link Game Center";

/** @var kUnlinkWithGameCenterTitle
@brief The title for the button to unlink with Game Center
*/
static NSString *const kUnlinkWithGameCenterTitle = @"Unlink Game Center";

/** @var kReauthenticateWithGameCenterTitle
@brief The title for the button to reauthenticate with Game Center
*/
static NSString *const kReauthenticateWithGameCenterTitle = @"Reauthenticate Game Center";

/** @var kNewOrExistingUserToggleTitle
@brief The title for button to enable new or existing user toggle.
*/
static NSString *const kNewOrExistingUserToggleTitle = @"New or Existing User Toggle";
Expand Down Expand Up @@ -707,6 +739,8 @@ - (id)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundl
}

- (void)viewDidLoad {
[super viewDidLoad];

// Give us a circle for the image view:
_userInfoTableViewCell.userInfoProfileURLImageView.layer.cornerRadius =
_userInfoTableViewCell.userInfoProfileURLImageView.frame.size.width / 2.0f;
Expand Down Expand Up @@ -745,6 +779,20 @@ - (void)updateTable {
_isNewUserToggleOn = !_isNewUserToggleOn;
[self updateTable]; }],
]],
[StaticContentTableViewSection sectionWithTitle:kGameCenterAuthSectionTitle cells:@[
[StaticContentTableViewCell cellWithTitle:kLogInWithSystemGameCenterTitle
action:^{ [weakSelf logInWithSystemGameCenter]; }],
[StaticContentTableViewCell cellWithTitle:kSignInWithGameCenterTitle
action:^{ [weakSelf signInWithGameCenter]; }],
[StaticContentTableViewCell cellWithTitle:kLinkWithGameCenterTitle
action:^{ [weakSelf linkWithGameCenter]; }],
[StaticContentTableViewCell cellWithTitle:kUnlinkWithGameCenterTitle
action:^{
[weakSelf unlinkFromProvider:FIRGameCenterAuthProviderID completion:nil];
}],
[StaticContentTableViewCell cellWithTitle:kReauthenticateWithGameCenterTitle
action:^{ [weakSelf reauthenticateWithGameCenter]; }],
]],
[StaticContentTableViewSection sectionWithTitle:kPhoneAuthSectionTitle cells:@[
[StaticContentTableViewCell cellWithTitle:kPhoneNumberSignInReCaptchaTitle
action:^{ [weakSelf signInWithPhoneNumberWithPrompt]; }],
Expand Down Expand Up @@ -1726,6 +1774,83 @@ - (void)signInFacebookAndRetrieveData {
[self signinWithProvider:[AuthProviders facebook] retrieveData:YES];
}

- (void)logInWithSystemGameCenter {
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController * _Nullable viewController,
NSError * _Nullable error) {
if (error) {
[self showTypicalUIForUserUpdateResultsWithTitle:@"Game Center Error" error:error];
} else if (viewController != nil) {
[self presentViewController:viewController animated:YES completion:nil];
}
};
}

- (void)signInWithGameCenter {
[FIRGameCenterAuthProvider getCredentialWithCompletion:
^(FIRAuthCredential * _Nullable credential, NSError * _Nullable error) {
if (error) {
[self showTypicalUIForUserUpdateResultsWithTitle:@"Game Center Error" error:error];
} else {
[[AppManager auth] signInAndRetrieveDataWithCredential:credential
completion:^(FIRAuthDataResult * _Nullable result,
NSError * _Nullable error) {
[self hideSpinner:^{
if (error) {
[self logFailure:@"Sign in with Game Center failed" error:error];
} else {
[self logSuccess:@"Sign in with Game Center succeeded."];
}
[self showTypicalUIForUserUpdateResultsWithTitle:@"Sign In Error" error:error];
}];
}];
}
}];
}

- (void)linkWithGameCenter {
[FIRGameCenterAuthProvider getCredentialWithCompletion:
^(FIRAuthCredential * _Nullable credential, NSError * _Nullable error) {
if (error) {
[self showTypicalUIForUserUpdateResultsWithTitle:@"Game Center Error" error:error];
} else {
[[self user] linkAndRetrieveDataWithCredential:credential
completion:^(FIRAuthDataResult * _Nullable result,
NSError * _Nullable error) {
[self hideSpinner:^{
if (error) {
[self logFailure:@"Link with Game Center failed" error:error];
} else {
[self logSuccess:@"Link with Game Center succeeded."];
}
[self showTypicalUIForUserUpdateResultsWithTitle:@"Link Error" error:error];
}];
}];
}
}];
}

- (void)reauthenticateWithGameCenter {
[FIRGameCenterAuthProvider getCredentialWithCompletion:
^(FIRAuthCredential * _Nullable credential, NSError * _Nullable error) {
if (error) {
[self showTypicalUIForUserUpdateResultsWithTitle:@"Game Center Error" error:error];
} else {
[[self user] reauthenticateAndRetrieveDataWithCredential:credential
completion:^(FIRAuthDataResult * _Nullable result,
NSError * _Nullable error) {
[self hideSpinner:^{
if (error) {
[self logFailure:@"Reauthenticate with Game Center failed" error:error];
} else {
[self logSuccess:@"Reauthenticate with Game Center succeeded."];
}
[self showTypicalUIForUserUpdateResultsWithTitle:@"Reauthenticate Error" error:error];
}];
}];
}
}];
}
/** @fn signInEmailPassword
@brief Invoked when "Sign in with Email/Password" row is pressed.
*/
Expand Down
Loading