FIRSegmentation.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2019 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "FirebaseSegmentation/Sources/Public/FIRSegmentation.h"
  15. #import "FirebaseCore/Sources/Private/FIRComponentContainer.h"
  16. #import "FirebaseCore/Sources/Private/FIRLogger.h"
  17. #import "FirebaseCore/Sources/Private/FIROptionsInternal.h"
  18. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  19. #import "FirebaseSegmentation/Sources/Private/FIRSegmentationComponent.h"
  20. #import "FirebaseSegmentation/Sources/SEGContentManager.h"
  21. @implementation FIRSegmentation {
  22. NSString *_firebaseAppName;
  23. SEGContentManager *_contentManager;
  24. }
  25. + (nonnull FIRSegmentation *)segmentation {
  26. if (![FIRApp isDefaultAppConfigured]) {
  27. [NSException
  28. raise:kFirebaseSegmentationErrorDomain
  29. format:@"FIRApp not configured. Please make sure you have called [FIRApp configure]"];
  30. }
  31. return [FIRSegmentation segmentationWithApp:[FIRApp defaultApp]];
  32. }
  33. + (nonnull FIRSegmentation *)segmentationWithApp:(nonnull FIRApp *)firebaseApp {
  34. // Use the provider to generate and return instances of FIRSegmentation for this specific app and
  35. // namespace. This will ensure the app is configured before Remote Config can return an instance.
  36. id<FIRSegmentationProvider> provider =
  37. FIR_COMPONENT(FIRSegmentationProvider, firebaseApp.container);
  38. return [provider segmentation];
  39. }
  40. - (void)setCustomInstallationID:(NSString *)customInstallationID
  41. completion:(void (^)(NSError *))completionHandler {
  42. [_contentManager
  43. associateCustomInstallationIdentiferNamed:customInstallationID
  44. firebaseApp:_firebaseAppName
  45. completion:^(BOOL success, NSDictionary *result) {
  46. if (!success) {
  47. // TODO(dmandar) log; pass along internal error code.
  48. NSError *error = [NSError
  49. errorWithDomain:kFirebaseSegmentationErrorDomain
  50. code:FIRSegmentationErrorCodeInternal
  51. userInfo:result];
  52. completionHandler(error);
  53. } else {
  54. completionHandler(nil);
  55. }
  56. }];
  57. }
  58. /// Designated initializer
  59. - (instancetype)initWithAppName:(NSString *)appName FIROptions:(FIROptions *)options {
  60. self = [super init];
  61. if (self) {
  62. _firebaseAppName = appName;
  63. // Initialize the content manager.
  64. _contentManager = [SEGContentManager sharedInstanceWithOptions:options];
  65. }
  66. return self;
  67. }
  68. @end