FIRSampleAppUtilities.m 4.6 KB

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