MainViewController+GameCenter.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "MainViewController+GameCenter.h"
  17. #import "AppManager.h"
  18. #import <FirebaseAuth/FirebaseAuth.h>
  19. #import "MainViewController+Internal.h"
  20. NS_ASSUME_NONNULL_BEGIN
  21. @implementation MainViewController (GameCenter)
  22. - (StaticContentTableViewSection *)gameCenterAuthSection {
  23. __weak typeof(self) weakSelf = self;
  24. return [StaticContentTableViewSection sectionWithTitle:@"Game Center Auth" cells:@[
  25. [StaticContentTableViewCell cellWithTitle:@"Log in System Game Center"
  26. action:^{ [weakSelf logInWithSystemGameCenter]; }],
  27. [StaticContentTableViewCell cellWithTitle:@"Sign in Game Center"
  28. action:^{ [weakSelf signInWithGameCenter]; }],
  29. [StaticContentTableViewCell cellWithTitle:@"Link Game Center"
  30. action:^{ [weakSelf linkWithGameCenter]; }],
  31. [StaticContentTableViewCell cellWithTitle:@"Unlink Game Center"
  32. action:^{ [weakSelf unlinkFromProvider:FIRGameCenterAuthProviderID completion:nil]; }],
  33. [StaticContentTableViewCell cellWithTitle:@"Reauthenticate Game Center"
  34. action:^{ [weakSelf reauthenticateWithGameCenter]; }],
  35. ]];
  36. }
  37. - (void)logInWithSystemGameCenter {
  38. GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
  39. localPlayer.authenticateHandler = ^(UIViewController * _Nullable viewController,
  40. NSError * _Nullable error) {
  41. if (error) {
  42. [self showTypicalUIForUserUpdateResultsWithTitle:@"Game Center Error" error:error];
  43. } else if (viewController != nil) {
  44. [self presentViewController:viewController animated:YES completion:nil];
  45. }
  46. };
  47. }
  48. - (void)signInWithGameCenter {
  49. [FIRGameCenterAuthProvider getCredentialWithCompletion:
  50. ^(FIRAuthCredential * _Nullable credential, NSError * _Nullable error) {
  51. if (error) {
  52. [self showTypicalUIForUserUpdateResultsWithTitle:@"Game Center Error" error:error];
  53. } else {
  54. [[AppManager auth] signInWithCredential:credential
  55. completion:^(FIRAuthDataResult * _Nullable result,
  56. NSError * _Nullable error) {
  57. [self hideSpinner:^{
  58. if (error) {
  59. [self logFailure:@"Sign in with Game Center failed" error:error];
  60. } else {
  61. [self logSuccess:@"Sign in with Game Center succeeded."];
  62. }
  63. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign In Error" error:error];
  64. }];
  65. }];
  66. }
  67. }];
  68. }
  69. - (void)linkWithGameCenter {
  70. [FIRGameCenterAuthProvider getCredentialWithCompletion:
  71. ^(FIRAuthCredential * _Nullable credential, NSError * _Nullable error) {
  72. if (error) {
  73. [self showTypicalUIForUserUpdateResultsWithTitle:@"Game Center Error" error:error];
  74. } else {
  75. [[self user] linkWithCredential:credential
  76. completion:^(FIRAuthDataResult * _Nullable result,
  77. NSError * _Nullable error) {
  78. [self hideSpinner:^{
  79. if (error) {
  80. [self logFailure:@"Link with Game Center failed" error:error];
  81. } else {
  82. [self logSuccess:@"Link with Game Center succeeded."];
  83. }
  84. [self showTypicalUIForUserUpdateResultsWithTitle:@"Link Error" error:error];
  85. }];
  86. }];
  87. }
  88. }];
  89. }
  90. - (void)reauthenticateWithGameCenter {
  91. [FIRGameCenterAuthProvider getCredentialWithCompletion:
  92. ^(FIRAuthCredential * _Nullable credential, NSError * _Nullable error) {
  93. if (error) {
  94. [self showTypicalUIForUserUpdateResultsWithTitle:@"Game Center Error" error:error];
  95. } else {
  96. [[self user] reauthenticateWithCredential:credential
  97. completion:^(FIRAuthDataResult * _Nullable result,
  98. NSError * _Nullable error) {
  99. [self hideSpinner:^{
  100. if (error) {
  101. [self logFailure:@"Reauthenticate with Game Center failed" error:error];
  102. } else {
  103. [self logSuccess:@"Reauthenticate with Game Center succeeded."];
  104. }
  105. [self showTypicalUIForUserUpdateResultsWithTitle:@"Reauthenticate Error" error:error];
  106. }];
  107. }];
  108. }
  109. }];
  110. }
  111. @end
  112. NS_ASSUME_NONNULL_END