FIRSampleAppUtilities.m 4.1 KB

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