FIRIAMBaseRenderingViewController.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  48. if (@available(iOS 13.0, *)) {
  49. [[NSNotificationCenter defaultCenter] addObserver:self
  50. selector:@selector(appWillBecomeInactive:)
  51. name:UISceneWillDeactivateNotification
  52. object:nil];
  53. [[NSNotificationCenter defaultCenter] addObserver:self
  54. selector:@selector(appDidBecomeActive:)
  55. name:UISceneDidActivateNotification
  56. object:nil];
  57. }
  58. #endif // defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  59. self.aggregateImpressionTimeInSeconds = 0;
  60. }
  61. - (void)viewDidAppear:(BOOL)animated {
  62. [super viewDidAppear:animated];
  63. [self impressionStartCheckpoint];
  64. }
  65. - (void)viewDidDisappear:(BOOL)animated {
  66. [super viewDidDisappear:animated];
  67. [self impressionStopCheckpoint];
  68. }
  69. // Call this when the view starts to be rendered so that we can track the aggregate impression
  70. // time for the current message
  71. - (void)impressionStartCheckpoint {
  72. self.currentImpressionStartTime = [self.timeFetcher currentTimestampInSeconds];
  73. [self setupMinImpressionTimer];
  74. }
  75. // Trigger this when the view stops to be rendered so that we can track the aggregate impression
  76. // time for the current message
  77. - (void)impressionStopCheckpoint {
  78. // Pause the impression timer.
  79. [self.minImpressionTimer invalidate];
  80. // Track the effective impression time for this impression session.
  81. double effectiveImpressionTime =
  82. [self.timeFetcher currentTimestampInSeconds] - self.currentImpressionStartTime;
  83. self.aggregateImpressionTimeInSeconds += effectiveImpressionTime;
  84. }
  85. - (void)dealloc {
  86. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID200001",
  87. @"[FIDBaseRenderingViewController dealloc] triggered");
  88. [self.minImpressionTimer invalidate];
  89. [NSNotificationCenter.defaultCenter removeObserver:self];
  90. }
  91. - (void)appWillBecomeInactive:(NSNotification *)notification {
  92. [self impressionStopCheckpoint];
  93. }
  94. - (void)appDidBecomeActive:(NSNotification *)notification {
  95. [self impressionStartCheckpoint];
  96. }
  97. - (void)minImpressionTimeReached {
  98. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID200004",
  99. @"Min impression time has been reached.");
  100. if ([self.displayDelegate respondsToSelector:@selector(impressionDetectedForMessage:)]) {
  101. [self.displayDelegate impressionDetectedForMessage:[self inAppMessage]];
  102. }
  103. [NSNotificationCenter.defaultCenter removeObserver:self];
  104. }
  105. - (void)setupMinImpressionTimer {
  106. NSTimeInterval remaining = kMinValidImpressionTime - self.aggregateImpressionTimeInSeconds;
  107. FIRLogDebug(kFIRLoggerInAppMessagingDisplay, @"I-FID200006",
  108. @"Remaining minimal impression time is %lf", remaining);
  109. if (remaining < 0.00001) {
  110. return;
  111. }
  112. __weak id weakSelf = self;
  113. self.minImpressionTimer =
  114. [NSTimer scheduledTimerWithTimeInterval:remaining
  115. target:weakSelf
  116. selector:@selector(minImpressionTimeReached)
  117. userInfo:nil
  118. repeats:NO];
  119. }
  120. - (void)dismissView:(FIRInAppMessagingDismissType)dismissType {
  121. [self.view.window setHidden:YES];
  122. // This is for the purpose of releasing the potential memory associated with the image view.
  123. self.view.window.rootViewController = nil;
  124. if (self.displayDelegate) {
  125. [self.displayDelegate messageDismissed:[self inAppMessage] dismissType:dismissType];
  126. } else {
  127. FIRLogWarning(kFIRLoggerInAppMessagingDisplay, @"I-FID200007",
  128. @"Display delegate is nil while message is being dismissed.");
  129. }
  130. return;
  131. }
  132. - (void)followAction:(FIRInAppMessagingAction *)action {
  133. [self.view.window setHidden:YES];
  134. // This is for the purpose of releasing the potential memory associated with the image view.
  135. self.view.window.rootViewController = nil;
  136. if (self.displayDelegate) {
  137. [self.displayDelegate messageClicked:[self inAppMessage] withAction:action];
  138. } else {
  139. FIRLogWarning(kFIRLoggerInAppMessagingDisplay, @"I-FID200008",
  140. @"Display delegate is nil while trying to follow action :%@.", action.actionText);
  141. }
  142. return;
  143. }
  144. @end
  145. #endif // TARGET_OS_IOS