Skip to content

Commit eca2170

Browse files
christibbsryanwilson
authored andcommitted
Update FIAM for iOS 13 Scene lifecycle paradigm (#3927)
* Add UIScene lifecycle notifications alongside UIApplication ones * Run styles.sh and update window helper to work on iOS 13 * Refactor foregrounded scene logic into its own method * Conditional compilation for iOS 13 code * Add notification parameter to notification selectors * Fix placeholder
1 parent 057d0df commit eca2170

9 files changed

+119
-27
lines changed

Firebase/InAppMessaging/Analytics/FIRIAMClearcutLogStorage.m

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,18 @@ - (instancetype)initWithExpireAfterInSeconds:(NSInteger)expireInSeconds
8989
_timeFetcher = timeFetcher;
9090
_recordExpiresInSeconds = expireInSeconds;
9191
[[NSNotificationCenter defaultCenter] addObserver:self
92-
selector:@selector(appWillBecomeInactive)
92+
selector:@selector(appWillBecomeInactive:)
9393
name:UIApplicationWillResignActiveNotification
9494
object:nil];
95+
#if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
96+
if (@available(iOS 13.0, *)) {
97+
[[NSNotificationCenter defaultCenter] addObserver:self
98+
selector:@selector(appWillBecomeInactive:)
99+
name:UISceneWillDeactivateNotification
100+
object:nil];
101+
}
102+
#endif // defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
103+
95104
@try {
96105
[self loadFromCachePath:cachePath];
97106
} @catch (NSException *exception) {
@@ -110,7 +119,7 @@ - (instancetype)initWithExpireAfterInSeconds:(NSInteger)expireInSeconds
110119
cachePath:nil];
111120
}
112121

113-
- (void)appWillBecomeInactive {
122+
- (void)appWillBecomeInactive:(NSNotification *)notification {
114123
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
115124
[self saveIntoCacheWithPath:nil];
116125
});

Firebase/InAppMessaging/Analytics/FIRIAMClearcutUploader.m

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,17 @@ - (instancetype)initWithRequestSender:(FIRIAMClearcutHttpRequestSender *)request
9595
_strategy = strategy;
9696
_queue = dispatch_queue_create("com.google.firebase.inappmessaging.clearcut_upload", NULL);
9797
[[NSNotificationCenter defaultCenter] addObserver:self
98-
selector:@selector(appWillEnterForeground:)
98+
selector:@selector(scheduleNextSendFromForeground:)
9999
name:UIApplicationWillEnterForegroundNotification
100100
object:nil];
101-
101+
#if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
102+
if (@available(iOS 13.0, *)) {
103+
[[NSNotificationCenter defaultCenter] addObserver:self
104+
selector:@selector(scheduleNextSendFromForeground:)
105+
name:UISceneWillEnterForegroundNotification
106+
object:nil];
107+
}
108+
#endif // defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
102109
_userDefaults = userDefaults ? userDefaults : [NSUserDefaults standardUserDefaults];
103110
// it would be 0 if it does not exist, which is equvilent to saying that
104111
// you can send now
@@ -121,7 +128,7 @@ - (void)dealloc {
121128
[[NSNotificationCenter defaultCenter] removeObserver:self];
122129
}
123130

124-
- (void)appWillEnterForeground:(UIApplication *)application {
131+
- (void)scheduleNextSendFromForeground:(NSNotification *)notification {
125132
FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM260010",
126133
@"App foregrounded, FIRIAMClearcutUploader will seed next send");
127134
[self scheduleNextSend];

Firebase/InAppMessaging/Flows/FIRIAMActivityLogger.m

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,17 @@ - (instancetype)initWithMaxCountBeforeReduce:(NSInteger)maxBeforeReduce
103103
_isDirty = NO;
104104

105105
[[NSNotificationCenter defaultCenter] addObserver:self
106-
selector:@selector(appWillBecomeInactive)
106+
selector:@selector(appWillBecomeInactive:)
107107
name:UIApplicationWillResignActiveNotification
108108
object:nil];
109-
109+
#if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
110+
if (@available(iOS 13.0, *)) {
111+
[[NSNotificationCenter defaultCenter] addObserver:self
112+
selector:@selector(appWillBecomeInactive:)
113+
name:UISceneWillDeactivateNotification
114+
object:nil];
115+
}
116+
#endif // defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
110117
if (loadFromCache) {
111118
@try {
112119
[self loadFromCachePath:nil];
@@ -162,7 +169,7 @@ - (BOOL)saveIntoCacheWithPath:(NSString *)cacheFilePath {
162169
}
163170
}
164171

165-
- (void)appWillBecomeInactive {
172+
- (void)appWillBecomeInactive:(NSNotification *)notification {
166173
FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM310004",
167174
@"App will become inactive, save"
168175
" activity logs");

Firebase/InAppMessaging/Flows/FIRIAMDisplayCheckOnAppForegroundFlow.m

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,23 @@ @implementation FIRIAMDisplayCheckOnAppForegroundFlow
2525
- (void)start {
2626
FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM500002",
2727
@"Start observing app foreground notifications for rendering messages.");
28-
[[NSNotificationCenter defaultCenter] addObserver:self
29-
selector:@selector(appWillEnterForeground:)
30-
name:UIApplicationWillEnterForegroundNotification
31-
object:nil];
28+
[[NSNotificationCenter defaultCenter]
29+
addObserver:self
30+
selector:@selector(checkAndDisplayNextAppForegroundMessageFromForeground:)
31+
name:UIApplicationWillEnterForegroundNotification
32+
object:nil];
33+
#if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
34+
if (@available(iOS 13.0, *)) {
35+
[[NSNotificationCenter defaultCenter]
36+
addObserver:self
37+
selector:@selector(checkAndDisplayNextAppForegroundMessageFromForeground:)
38+
name:UISceneWillEnterForegroundNotification
39+
object:nil];
40+
}
41+
#endif // defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
3242
}
3343

34-
- (void)appWillEnterForeground:(UIApplication *)application {
44+
- (void)checkAndDisplayNextAppForegroundMessageFromForeground:(NSNotification *)notification {
3545
FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM500001",
3646
@"App foregrounded, wake up to check in-app messaging.");
3747

Firebase/InAppMessaging/Flows/FIRIAMFetchOnAppForegroundFlow.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,17 @@ - (void)start {
2626
selector:@selector(appWillEnterForeground:)
2727
name:UIApplicationWillEnterForegroundNotification
2828
object:nil];
29+
#if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
30+
if (@available(iOS 13.0, *)) {
31+
[[NSNotificationCenter defaultCenter] addObserver:self
32+
selector:@selector(appWillEnterForeground:)
33+
name:UISceneWillEnterForegroundNotification
34+
object:nil];
35+
}
36+
#endif // defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
2937
}
3038

31-
- (void)appWillEnterForeground:(UIApplication *)application {
39+
- (void)appWillEnterForeground:(NSNotification *)notification {
3240
FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM600001",
3341
@"App foregrounded, wake up to see if we can fetch in-app messaging.");
3442
// for fetch operation, dispatch it to non main UI thread to avoid blocking. It's ok to dispatch

Firebase/InAppMessagingDisplay/Banner/FIDBannerViewController.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,13 @@ - (void)setupAutoDismissTimer {
283283
}
284284

285285
// Handlers for app become active inactive so that we can better adjust our auto dismiss feature
286-
- (void)appDidBecomeInactive:(UIApplication *)application {
287-
[super appDidBecomeInactive:application];
286+
- (void)appWillBecomeInactive:(NSNotification *)notification {
287+
[super appWillBecomeInactive:notification];
288288
[self.autoDismissTimer invalidate];
289289
}
290290

291-
- (void)appDidBecomeActive:(UIApplication *)application {
292-
[super appDidBecomeActive:application];
291+
- (void)appDidBecomeActive:(NSNotification *)notification {
292+
[super appDidBecomeActive:notification];
293293
[self setupAutoDismissTimer];
294294
}
295295

Firebase/InAppMessagingDisplay/FIDBaseRenderingViewController.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ NS_ASSUME_NONNULL_BEGIN
3030
// These are the two methods we use to respond to app state change for the purpose of
3131
// actual display time tracking. Subclass can override this one to have more logic for responding
3232
// to the two events, but remember to trigger super's implementation.
33-
- (void)appDidBecomeInactive:(UIApplication *)application;
34-
- (void)appDidBecomeActive:(UIApplication *)application;
33+
- (void)appWillBecomeInactive:(NSNotification *)notification;
34+
- (void)appDidBecomeActive:(NSNotification *)notification;
3535

3636
// Tracking the aggregate impression time for the rendered message. Used to determine when
3737
// we are eaching the minimal iimpression time requirements. Exposed so that sub banner vc

Firebase/InAppMessagingDisplay/FIDBaseRenderingViewController.m

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,27 @@ - (void)viewDidLoad {
4444
// app foreground/background events since viewDidAppear/viewDidDisappear are not
4545
// triggered when app switches happen.
4646
[[NSNotificationCenter defaultCenter] addObserver:self
47-
selector:@selector(appDidBecomeInactive:)
47+
selector:@selector(appWillBecomeInactive:)
4848
name:UIApplicationWillResignActiveNotification
4949
object:nil];
5050

5151
[[NSNotificationCenter defaultCenter] addObserver:self
5252
selector:@selector(appDidBecomeActive:)
5353
name:UIApplicationDidBecomeActiveNotification
5454
object:nil];
55-
55+
#if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
56+
if (@available(iOS 13.0, *)) {
57+
[[NSNotificationCenter defaultCenter] addObserver:self
58+
selector:@selector(appWillBecomeInactive:)
59+
name:UISceneWillDeactivateNotification
60+
object:nil];
61+
62+
[[NSNotificationCenter defaultCenter] addObserver:self
63+
selector:@selector(appDidBecomeActive:)
64+
name:UISceneDidActivateNotification
65+
object:nil];
66+
}
67+
#endif // defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
5668
self.aggregateImpressionTimeInSeconds = 0;
5769
}
5870

@@ -92,11 +104,11 @@ - (void)dealloc {
92104
[NSNotificationCenter.defaultCenter removeObserver:self];
93105
}
94106

95-
- (void)appDidBecomeInactive:(UIApplication *)application {
107+
- (void)appWillBecomeInactive:(NSNotification *)notification {
96108
[self impressionStopCheckpoint];
97109
}
98110

99-
- (void)appDidBecomeActive:(UIApplication *)application {
111+
- (void)appDidBecomeActive:(NSNotification *)notification {
100112
[self impressionStartCheckpoint];
101113
}
102114

Firebase/InAppMessagingDisplay/FIDRenderingWindowHelper.m

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,16 @@ + (UIWindow *)UIWindowForModalView {
2424
static dispatch_once_t onceToken;
2525

2626
dispatch_once(&onceToken, ^{
27-
UIWindowForModal = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
27+
#if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
28+
if (@available(iOS 13.0, *)) {
29+
UIWindowScene *foregroundedScene = [[self class] foregroundedScene];
30+
UIWindowForModal = [[UIWindow alloc] initWithWindowScene:foregroundedScene];
31+
} else {
32+
#endif // defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
33+
UIWindowForModal = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
34+
#if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
35+
}
36+
#endif // defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
2837
UIWindowForModal.windowLevel = UIWindowLevelNormal;
2938
});
3039
return UIWindowForModal;
@@ -35,7 +44,17 @@ + (UIWindow *)UIWindowForBannerView {
3544
static dispatch_once_t onceToken;
3645

3746
dispatch_once(&onceToken, ^{
38-
UIWindowForBanner = [[FIDBannerViewUIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
47+
#if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
48+
if (@available(iOS 13.0, *)) {
49+
UIWindowScene *foregroundedScene = [[self class] foregroundedScene];
50+
UIWindowForBanner = [[FIDBannerViewUIWindow alloc] initWithWindowScene:foregroundedScene];
51+
} else {
52+
#endif // defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
53+
UIWindowForBanner =
54+
[[FIDBannerViewUIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
55+
#if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
56+
}
57+
#endif
3958
UIWindowForBanner.windowLevel = UIWindowLevelNormal;
4059
});
4160

@@ -47,10 +66,30 @@ + (UIWindow *)UIWindowForImageOnlyView {
4766
static dispatch_once_t onceToken;
4867

4968
dispatch_once(&onceToken, ^{
50-
UIWindowForImageOnly = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
69+
#if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
70+
if (@available(iOS 13.0, *)) {
71+
UIWindowScene *foregroundedScene = [[self class] foregroundedScene];
72+
UIWindowForImageOnly = [[UIWindow alloc] initWithWindowScene:foregroundedScene];
73+
} else {
74+
#endif // defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
75+
UIWindowForImageOnly = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
76+
#if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
77+
}
78+
#endif // defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
5179
UIWindowForImageOnly.windowLevel = UIWindowLevelNormal;
5280
});
5381

5482
return UIWindowForImageOnly;
5583
}
84+
85+
#if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
86+
+ (UIWindowScene *)foregroundedScene API_AVAILABLE(ios(13.0)) {
87+
for (UIWindowScene *connectedScene in [UIApplication sharedApplication].connectedScenes) {
88+
if (connectedScene.activationState == UISceneActivationStateForegroundActive) {
89+
return connectedScene;
90+
}
91+
}
92+
return nil;
93+
}
94+
#endif
5695
@end

0 commit comments

Comments
 (0)