FIDBaseRenderingViewController.m 5.5 KB

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