No Description

FIRGameCenterAuthProvider.m 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. NS_ASSUME_NONNULL_BEGIN
  22. @implementation FIRGameCenterAuthProvider
  23. - (instancetype)init {
  24. [FIRAuthExceptionUtils raiseMethodNotImplementedExceptionWithReason:
  25. @"This class is not meant to be initialized."];
  26. return nil;
  27. }
  28. + (void)getCredentialWithCompletion:(FIRGameCenterCredentialCallback)completion {
  29. /**
  30. Linking GameKit.framework without using it on macOS results in App Store rejection.
  31. Thus we don't link GameKit.framework to our SDK directly. `optionalLocalPlayer` is used for
  32. checking whether the APP that consuming our SDK has linked GameKit.framework. If not, a
  33. `GameKitNotLinkedError` will be raised.
  34. **/
  35. GKLocalPlayer * _Nullable optionalLocalPlayer = [[NSClassFromString(@"GKLocalPlayer") alloc] init];
  36. if (!optionalLocalPlayer) {
  37. if (completion) {
  38. completion(nil, [FIRAuthErrorUtils gameKitNotLinkedError]);
  39. }
  40. return;
  41. }
  42. __weak GKLocalPlayer *localPlayer = [[optionalLocalPlayer class] localPlayer];
  43. if (!localPlayer.isAuthenticated) {
  44. if (completion) {
  45. completion(nil, [FIRAuthErrorUtils localPlayerNotAuthenticatedError]);
  46. }
  47. return;
  48. }
  49. [localPlayer generateIdentityVerificationSignatureWithCompletionHandler:
  50. ^(NSURL *publicKeyURL, NSData *signature, NSData *salt, uint64_t timestamp, NSError *error) {
  51. if (error) {
  52. if (completion) {
  53. completion(nil, error);
  54. }
  55. } else {
  56. if (completion) {
  57. /**
  58. @c `localPlayer.alias` is actually the displayname needed, instead of
  59. `localPlayer.displayname`. For more information, check
  60. https://developer.apple.com/documentation/gamekit/gkplayer
  61. **/
  62. NSString *displayName = localPlayer.alias;
  63. FIRGameCenterAuthCredential *credential =
  64. [[FIRGameCenterAuthCredential alloc] initWithPlayerID:localPlayer.playerID
  65. publicKeyURL:publicKeyURL
  66. signature:signature
  67. salt:salt
  68. timestamp:timestamp
  69. displayName:displayName];
  70. completion(credential, nil);
  71. }
  72. }
  73. }];
  74. }
  75. @end
  76. NS_ASSUME_NONNULL_END