FIRSampleAppUtilities.m 4.5 KB

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