FIRIAMBaseRenderingViewController.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright 2018 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 <TargetConditionals.h>
  17. #if TARGET_OS_IOS
  18. #import "FirebaseInAppMessaging/Sources/DefaultUI/FIRCore+InAppMessagingDisplay.h"
  19. #import "FirebaseInAppMessaging/Sources/DefaultUI/FIRIAMBaseRenderingViewController.h"
  20. #import "FirebaseInAppMessaging/Sources/Private/Util/FIRIAMTimeFetcher.h"
  21. @interface FIRIAMBaseRenderingViewController ()
  22. // For fiam messages, it's required to be kMinValidImpressionTime to
  23. // be considered as a valid impression help. If the app is closed before that's reached,
  24. // SDK may try to render the same message again in the future.
  25. @property(nonatomic, nullable) NSTimer *minImpressionTimer;
  26. // Tracking the start time when the current impression session start.
  27. @property(nonatomic) double currentImpressionStartTime;
  28. @end
  29. static const NSTimeInterval kMinValidImpressionTime = 3.0;
  30. @implementation FIRIAMBaseRenderingViewController
  31. - (nullable FIRInAppMessagingDisplayMessage *)inAppMessage {
  32. return nil;
  33. }
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. // In order to track display time for this message, we need to respond to
  37. // app foreground/background events since viewDidAppear/viewDidDisappear are not
  38. // triggered when app switches happen.
  39. [[NSNotificationCenter defaultCenter] addObserver:self
  40. selector:@selector(appWillBecomeInactive:)
  41. name:UIApplicationWillResignActiveNotification
  42. object:nil];
  43. [[NSNotificationCenter defaultCenter] addObserver:self
  44. selector:@selector(appDidBecomeActive:)
  45. name:UIApplicationDidBecomeActiveNotification
  46. object:nil];
  47. if (@available(iOS 13.0, tvOS 13.0, *)) {
  48. [[NSNotificationCenter defaultCenter] addObserver:self
  49. selector:@selector(appWillBecomeInactive:)
  50. name:UISceneWillDeactivateNotification
  51. object:nil];
  52. [[NSNotificationCenter defaultCenter] addObserver:self
  53. selector:@selector(appDidBecomeActive:)
  54. name:UISceneDidActivateNotification
  55. object:nil];
  56. }
  57. self.aggregateImpressionTimeInSeconds = 0;
  58. }
  59. - (void)viewDidAppear:(BOOL)animated {
  60. [super viewDidAppear:animated];
  61. [self impressionStartCheckpoint];
  62. }
  63. - (void)viewDidDisappear:(BOOL)animated {
  64. [super viewDidDisappear:animated];
  65. [self impressionStopCheckpoint];
  66. }
  67. // Call this when the view starts to be rendered so that we can track the aggregate impression
  68. // time for the current message
  69. - (void)impressionStartCheckpoint {
  70. self.currentImpressionStartTime = [self.timeFetcher currentTimestampInSeconds];
  71. [self setupMinImpressionTimer];
  72. }
  73. // Trigger this when the view stops to be rendered so that we can track the aggregate impression
  74. // time for the current message
  75. - (void)impressionStopCheckpoint {
  76. // Pause the impression timer.
  77. [self.minImpressionTimer invalidate];
  78. // Track the effective impression time for this impression session.
  79. double effectiveImpressionTime =
  80. [self.timeFetcher currentTimestampInSeconds] - self.currentImpressionStartTime;
  81. self.aggregateImpressionTimeInSeconds += effectiveImpressionTime;
  82. }
  83. - (void)dealloc {
  84. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID200001",
  85. @"[FIDBaseRenderingViewController dealloc] triggered");
  86. [self.minImpressionTimer invalidate];
  87. [NSNotificationCenter.defaultCenter removeObserver:self];
  88. }
  89. - (void)appWillBecomeInactive:(NSNotification *)notification {
  90. [self impressionStopCheckpoint];
  91. }
  92. - (void)appDidBecomeActive:(NSNotification *)notification {
  93. [self impressionStartCheckpoint];
  94. }
  95. - (void)minImpressionTimeReached {
  96. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID200004",
  97. @"Min impression time has been reached.");
  98. if ([self.displayDelegate respondsToSelector:@selector(impressionDetectedForMessage:)]) {
  99. [self.displayDelegate impressionDetectedForMessage:[self inAppMessage]];
  100. }
  101. [NSNotificationCenter.defaultCenter removeObserver:self];
  102. }
  103. - (void)setupMinImpressionTimer {
  104. NSTimeInterval remaining = kMinValidImpressionTime - self.aggregateImpressionTimeInSeconds;
  105. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID200006",
  106. @"Remaining minimal impression time is %lf", remaining);
  107. if (remaining < 0.00001) {
  108. return;
  109. }
  110. __weak id weakSelf = self;
  111. self.minImpressionTimer =
  112. [NSTimer scheduledTimerWithTimeInterval:remaining
  113. target:weakSelf
  114. selector:@selector(minImpressionTimeReached)
  115. userInfo:nil
  116. repeats:NO];
  117. }
  118. - (void)dismissView:(FIRInAppMessagingDismissType)dismissType {
  119. [self.view.window setHidden:YES];
  120. // This is for the purpose of releasing the potential memory associated with the image view.
  121. self.view.window.rootViewController = nil;
  122. if (self.displayDelegate) {
  123. [self.displayDelegate messageDismissed:[self inAppMessage] dismissType:dismissType];
  124. } else {
  125. FIRLogWarning(kFIRLoggerInAppMessagingDisplay, @"I-FID200007",
  126. @"Display delegate is nil while message is being dismissed.");
  127. }
  128. return;
  129. }
  130. - (void)followAction:(FIRInAppMessagingAction *)action {
  131. [self.view.window setHidden:YES];
  132. // This is for the purpose of releasing the potential memory associated with the image view.
  133. self.view.window.rootViewController = nil;
  134. if (self.displayDelegate) {
  135. [self.displayDelegate messageClicked:[self inAppMessage] withAction:action];
  136. } else {
  137. FIRLogWarning(kFIRLoggerInAppMessagingDisplay, @"I-FID200008",
  138. @"Display delegate is nil while trying to follow action :%@.", action.actionText);
  139. }
  140. return;
  141. }
  142. @end
  143. #endif // TARGET_OS_IOS