FIRSegmentation.m 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. FIRLogError(kFIRLoggerSegmentation, @"I-SEG000001",
  28. @"FIRApp not configured. Please make sure you have called [FIRApp configure]");
  29. }
  30. return [FIRSegmentation segmentationWithApp:[FIRApp defaultApp]];
  31. }
  32. + (nonnull FIRSegmentation *)segmentationWithApp:(nonnull FIRApp *)firebaseApp {
  33. // Use the provider to generate and return instances of FIRSegmentation for this specific app and
  34. // namespace. This will ensure the app is configured before Remote Config can return an instance.
  35. id<FIRSegmentationProvider> provider =
  36. FIR_COMPONENT(FIRSegmentationProvider, firebaseApp.container);
  37. return [provider segmentation];
  38. }
  39. - (void)setCustomInstallationID:(NSString *)customInstallationID
  40. completion:(void (^)(NSError *))completionHandler {
  41. [_contentManager
  42. associateCustomInstallationIdentiferNamed:customInstallationID
  43. firebaseApp:_firebaseAppName
  44. completion:^(BOOL success, NSDictionary *result) {
  45. if (!success) {
  46. // TODO(dmandar) log; pass along internal error code.
  47. NSError *error = [NSError
  48. errorWithDomain:kFirebaseSegmentationErrorDomain
  49. code:FIRSegmentationErrorCodeInternal
  50. userInfo:result];
  51. completionHandler(error);
  52. } else {
  53. completionHandler(nil);
  54. }
  55. }];
  56. }
  57. /// Designated initializer
  58. - (instancetype)initWithAppName:(NSString *)appName FIROptions:(FIROptions *)options {
  59. self = [super init];
  60. if (self) {
  61. _firebaseAppName = appName;
  62. // Initialize the content manager.
  63. _contentManager = [SEGContentManager sharedInstanceWithOptions:options];
  64. }
  65. return self;
  66. }
  67. @end