FIRGameCenterAuthProvider.m 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright 2018 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 "FIRGameCenterAuthProvider.h"
  17. #import <GameKit/GameKit.h>
  18. #import "FIRAuthErrorUtils.h"
  19. #import "FIRAuthExceptionUtils.h"
  20. #import "FIRGameCenterAuthCredential.h"
  21. @implementation FIRGameCenterAuthProvider
  22. - (instancetype)init {
  23. [FIRAuthExceptionUtils raiseMethodNotImplementedExceptionWithReason:
  24. @"This class is not meant to be initialized."];
  25. return nil;
  26. }
  27. + (void)getCredentialWithCompletion:(FIRGameCenterCredentialCallback)completion {
  28. __weak GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
  29. if (!localPlayer.isAuthenticated) {
  30. if (completion) {
  31. completion(nil, [FIRAuthErrorUtils localPlayerNotAuthenticatedError]);
  32. }
  33. return;
  34. }
  35. [localPlayer generateIdentityVerificationSignatureWithCompletionHandler:
  36. ^(NSURL *publicKeyURL, NSData *signature, NSData *salt, uint64_t timestamp, NSError *error) {
  37. if (error) {
  38. if (completion) {
  39. completion(nil, error);
  40. }
  41. } else {
  42. if (completion) {
  43. /**
  44. @c `localPlayer.alias` is actually the displayname needed, instead of
  45. `localPlayer.displayname`. For more information, check
  46. https://developer.apple.com/documentation/gamekit/gkplayer
  47. **/
  48. NSString *displayName = localPlayer.alias;
  49. FIRGameCenterAuthCredential *credential =
  50. [[FIRGameCenterAuthCredential alloc] initWithPlayerID:localPlayer.playerID
  51. publicKeyURL:publicKeyURL
  52. signature:signature
  53. salt:salt
  54. timestamp:timestamp
  55. displayName:displayName];
  56. completion(credential, nil);
  57. }
  58. }
  59. }];
  60. }
  61. @end