FIRSampleAppUtilities.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #import "Example/Shared/FIRSampleAppUtilities.h"
  17. #import <SafariServices/SafariServices.h>
  18. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  19. NSString *const kGoogleAppIDPlistKey = @"GOOGLE_APP_ID";
  20. // Dummy plist GOOGLE_APP_ID
  21. NSString *const kDummyGoogleAppID = @"1:123:ios:123abc";
  22. // Github Repo URL String
  23. NSString *const kGithubRepoURLString = @"https://github.com/firebase/firebase-ios-sdk/";
  24. // Alert contents
  25. NSString *const kInvalidPlistAlertTitle = @"GoogleService-Info.plist";
  26. NSString *const kInvalidPlistAlertMessage = @"This sample app needs to be updated with a valid "
  27. @"GoogleService-Info.plist file in order to configure "
  28. @"Firebase.\n\n"
  29. @"Please update the app with a valid plist file, "
  30. @"following the instructions in the Firebase Github "
  31. @"repository at: %@";
  32. @implementation FIRSampleAppUtilities
  33. + (BOOL)appContainsRealServiceInfoPlist {
  34. static BOOL containsRealServiceInfoPlist = NO;
  35. static dispatch_once_t onceToken;
  36. dispatch_once(&onceToken, ^{
  37. NSBundle *bundle = [NSBundle mainBundle];
  38. containsRealServiceInfoPlist = [self containsRealServiceInfoPlistInBundle:bundle];
  39. });
  40. return containsRealServiceInfoPlist;
  41. }
  42. + (BOOL)containsRealServiceInfoPlistInBundle:(NSBundle *)bundle {
  43. NSString *bundlePath = bundle.bundlePath;
  44. if (!bundlePath.length) {
  45. return NO;
  46. }
  47. NSString *plistFilePath = [bundle pathForResource:kServiceInfoFileName
  48. ofType:kServiceInfoFileType];
  49. if (!plistFilePath.length) {
  50. return NO;
  51. }
  52. NSDictionary *plist = [NSDictionary dictionaryWithContentsOfFile:plistFilePath];
  53. if (!plist) {
  54. return NO;
  55. }
  56. // Perform a very naive validation by checking to see if the plist has the dummy google app id
  57. NSString *googleAppID = plist[kGoogleAppIDPlistKey];
  58. if (!googleAppID.length) {
  59. return NO;
  60. }
  61. if ([googleAppID isEqualToString:kDummyGoogleAppID]) {
  62. return NO;
  63. }
  64. return YES;
  65. }
  66. + (void)presentAlertForInvalidServiceInfoPlistFromViewController:
  67. (UIViewController *)viewController {
  68. NSString *message = [NSString stringWithFormat:kInvalidPlistAlertMessage, kGithubRepoURLString];
  69. UIAlertController *alertController =
  70. [UIAlertController alertControllerWithTitle:kInvalidPlistAlertTitle
  71. message:message
  72. preferredStyle:UIAlertControllerStyleAlert];
  73. UIAlertAction *viewReadmeAction = [UIAlertAction
  74. actionWithTitle:@"View Github"
  75. style:UIAlertActionStyleDefault
  76. handler:^(UIAlertAction *_Nonnull action) {
  77. NSURL *githubURL = [NSURL URLWithString:kGithubRepoURLString];
  78. [FIRSampleAppUtilities navigateToURL:githubURL fromViewController:viewController];
  79. }];
  80. [alertController addAction:viewReadmeAction];
  81. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Close"
  82. style:UIAlertActionStyleCancel
  83. handler:nil];
  84. [alertController addAction:cancelAction];
  85. [viewController presentViewController:alertController animated:YES completion:nil];
  86. }
  87. + (void)navigateToURL:(NSURL *)url fromViewController:(UIViewController *)viewController {
  88. if ([SFSafariViewController class]) {
  89. SFSafariViewController *safariController = [[SFSafariViewController alloc] initWithURL:url];
  90. [viewController showDetailViewController:safariController sender:nil];
  91. } else {
  92. [[UIApplication sharedApplication] openURL:url];
  93. }
  94. }
  95. @end