FIRSampleAppUtilities.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "FIRSampleAppUtilities.h"
  17. #import <FirebaseCore/FIROptionsInternal.h>
  18. #import <SafariServices/SafariServices.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 =
  27. @"This sample app needs to be updated with a valid "
  28. @"GoogleService-Info.plist file in order to configure "
  29. @"Firebase.\n\n"
  30. @"Please update the app with a valid plist file, "
  31. @"following the instructions in the Firebase Github "
  32. @"repository at: %@";
  33. @implementation FIRSampleAppUtilities
  34. + (BOOL)appContainsRealServiceInfoPlist {
  35. static BOOL containsRealServiceInfoPlist = NO;
  36. static dispatch_once_t onceToken;
  37. dispatch_once(&onceToken, ^{
  38. NSBundle *bundle = [NSBundle mainBundle];
  39. containsRealServiceInfoPlist = [self containsRealServiceInfoPlistInBundle:bundle];
  40. });
  41. return containsRealServiceInfoPlist;
  42. }
  43. + (BOOL)containsRealServiceInfoPlistInBundle:(NSBundle *)bundle {
  44. NSString *bundlePath = bundle.bundlePath;
  45. if (!bundlePath.length) {
  46. return NO;
  47. }
  48. NSString *plistFilePath =
  49. [bundle pathForResource:kServiceInfoFileName ofType:kServiceInfoFileType];
  50. if (!plistFilePath.length) {
  51. return NO;
  52. }
  53. NSDictionary *plist = [NSDictionary dictionaryWithContentsOfFile:plistFilePath];
  54. if (!plist) {
  55. return NO;
  56. }
  57. // Perform a very naive validation by checking to see if the plist has the dummy google app id
  58. NSString *googleAppID = plist[kGoogleAppIDPlistKey];
  59. if (!googleAppID.length) {
  60. return NO;
  61. }
  62. if ([googleAppID isEqualToString:kDummyGoogleAppID]) {
  63. return NO;
  64. }
  65. return YES;
  66. }
  67. + (void)presentAlertForInvalidServiceInfoPlistFromViewController:
  68. (UIViewController *)viewController {
  69. NSString *message = [NSString stringWithFormat:kInvalidPlistAlertMessage, kGithubRepoURLString];
  70. UIAlertController *alertController =
  71. [UIAlertController alertControllerWithTitle:kInvalidPlistAlertTitle
  72. message:message
  73. preferredStyle:UIAlertControllerStyleAlert];
  74. UIAlertAction *viewReadmeAction = [UIAlertAction
  75. actionWithTitle:@"View Github"
  76. style:UIAlertActionStyleDefault
  77. handler:^(UIAlertAction *_Nonnull action) {
  78. NSURL *githubURL = [NSURL URLWithString:kGithubRepoURLString];
  79. [FIRSampleAppUtilities navigateToURL:githubURL fromViewController:viewController];
  80. }];
  81. [alertController addAction:viewReadmeAction];
  82. UIAlertAction *cancelAction =
  83. [UIAlertAction actionWithTitle:@"Close" style:UIAlertActionStyleCancel 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