FUNInstanceIDProxy.m 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright 2017 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. // Note: This is forked from FIRMessagingInstanceIDProxy.m
  17. #import "FUNInstanceIDProxy.h"
  18. @implementation FUNInstanceIDProxy
  19. + (nonnull instancetype)instanceIDProxy {
  20. static id proxyInstanceID = nil;
  21. static dispatch_once_t onceToken;
  22. dispatch_once(&onceToken, ^{
  23. Class instanceIDClass = NSClassFromString(@"FIRInstanceID");
  24. if (!instanceIDClass) {
  25. proxyInstanceID = nil;
  26. return;
  27. }
  28. SEL instanceIDSelector = NSSelectorFromString(@"instanceID");
  29. if (![instanceIDClass respondsToSelector:instanceIDSelector]) {
  30. proxyInstanceID = nil;
  31. return;
  32. }
  33. IMP instanceIDImp = [instanceIDClass methodForSelector:instanceIDSelector];
  34. id (*instanceIDFunc)(id, SEL) = (void *)instanceIDImp;
  35. proxyInstanceID = instanceIDFunc(instanceIDClass, instanceIDSelector);
  36. });
  37. return (FUNInstanceIDProxy *)proxyInstanceID;
  38. }
  39. #pragma mark - Tokens
  40. - (nullable NSString *)token {
  41. id proxy = [[self class] instanceIDProxy];
  42. SEL getTokenSelector = NSSelectorFromString(@"token");
  43. if (![proxy respondsToSelector:getTokenSelector]) {
  44. return nil;
  45. }
  46. IMP getTokenIMP = [proxy methodForSelector:getTokenSelector];
  47. NSString *(*getToken)(id, SEL) = (void *)getTokenIMP;
  48. return getToken(proxy, getTokenSelector);
  49. }
  50. @end