Skip to content

Commit 2c81997

Browse files
authored
FirebaseInAppMessaging: Fix crash on message presentation while a CarPlay scene is running (#11232)
1 parent 8056cda commit 2c81997

File tree

5 files changed

+78
-18
lines changed

5 files changed

+78
-18
lines changed

FirebaseInAppMessaging/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 10.10.0
2+
- [fixed] Crash on InApp message presentation when a CarPlay scene is active (#9376)
3+
14
# 10.0.0
25
- [removed] Removed `foo` constant from Swift `InAppMessagingPreviewHelpers` API (#10222).
36
- [fixed] Changed internal `dataChanged` symbol that triggered App Store warnings (#10276).

FirebaseInAppMessaging/Sources/DefaultUI/FIRIAMRenderingWindowHelper.m

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#import "FirebaseInAppMessaging/Sources/DefaultUI/Banner/FIRIAMBannerViewUIWindow.h"
2121
#import "FirebaseInAppMessaging/Sources/DefaultUI/FIRIAMRenderingWindowHelper.h"
22+
#import "FirebaseInAppMessaging/Sources/Private/Util/UIApplication+FIRForegroundWindowScene.h"
2223

2324
@implementation FIRIAMRenderingWindowHelper
2425

@@ -63,17 +64,8 @@ + (UIWindow *)windowForNonBlockingView {
6364
}
6465

6566
#if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
66-
+ (UIWindowScene *)foregroundedScene API_AVAILABLE(ios(13.0)) {
67-
for (UIWindowScene *connectedScene in [UIApplication sharedApplication].connectedScenes) {
68-
if (connectedScene.activationState == UISceneActivationStateForegroundActive) {
69-
return connectedScene;
70-
}
71-
}
72-
return nil;
73-
}
74-
7567
+ (UIWindow *)iOS13PlusWindow API_AVAILABLE(ios(13.0)) {
76-
UIWindowScene *foregroundedScene = [[self class] foregroundedScene];
68+
UIWindowScene *foregroundedScene = [[UIApplication sharedApplication] fir_foregroundWindowScene];
7769
if (foregroundedScene.delegate) {
7870
return [[UIWindow alloc] initWithWindowScene:foregroundedScene];
7971
} else {
@@ -82,7 +74,7 @@ + (UIWindow *)iOS13PlusWindow API_AVAILABLE(ios(13.0)) {
8274
}
8375

8476
+ (FIRIAMBannerViewUIWindow *)iOS13PlusBannerWindow API_AVAILABLE(ios(13.0)) {
85-
UIWindowScene *foregroundedScene = [[self class] foregroundedScene];
77+
UIWindowScene *foregroundedScene = [[UIApplication sharedApplication] fir_foregroundWindowScene];
8678
if (foregroundedScene.delegate) {
8779
return [[FIRIAMBannerViewUIWindow alloc] initWithWindowScene:foregroundedScene];
8880
} else {

FirebaseInAppMessaging/Sources/Flows/FIRIAMDisplayExecutor.m

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#import "FirebaseInAppMessaging/Sources/Private/Data/FIRIAMMessageDefinition.h"
2626
#import "FirebaseInAppMessaging/Sources/Private/Flows/FIRIAMActivityLogger.h"
2727
#import "FirebaseInAppMessaging/Sources/Private/Flows/FIRIAMDisplayExecutor.h"
28+
#import "FirebaseInAppMessaging/Sources/Private/Util/UIApplication+FIRForegroundWindowScene.h"
2829
#import "FirebaseInAppMessaging/Sources/Public/FirebaseInAppMessaging/FIRInAppMessaging.h"
2930
#import "FirebaseInAppMessaging/Sources/RenderingObjects/FIRInAppMessagingRenderingPrivate.h"
3031
#import "FirebaseInAppMessaging/Sources/Runtime/FIRIAMSDKRuntimeErrorCodes.h"
@@ -356,13 +357,8 @@ - (void)displayMessageLoadError:(NSError *)error {
356357
dispatch_async(dispatch_get_main_queue(), ^{
357358
#if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
358359
if (@available(iOS 13.0, tvOS 13.0, *)) {
359-
UIWindowScene *foregroundedScene = nil;
360-
for (UIWindowScene *connectedScene in [UIApplication sharedApplication].connectedScenes) {
361-
if (connectedScene.activationState == UISceneActivationStateForegroundActive) {
362-
foregroundedScene = connectedScene;
363-
break;
364-
}
365-
}
360+
UIWindowScene *foregroundedScene =
361+
[[UIApplication sharedApplication] fir_foregroundWindowScene];
366362

367363
if (foregroundedScene == nil) {
368364
return;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2023 Google LLC
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+
17+
#import <Foundation/Foundation.h>
18+
#import <UIKit/UIKit.h>
19+
20+
NS_ASSUME_NONNULL_BEGIN
21+
22+
// Extension on UIApplication to get the first foreground connected scene
23+
@interface UIApplication (FIRForegroundWindowScene)
24+
25+
- (nullable UIWindowScene *)fir_foregroundWindowScene API_AVAILABLE(ios(13.0));
26+
27+
@end
28+
29+
NS_ASSUME_NONNULL_END
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2023 Google LLC
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+
17+
#import <TargetConditionals.h>
18+
#if TARGET_OS_IOS || TARGET_OS_TV
19+
20+
#import "FirebaseInAppMessaging/Sources/Private/Util/UIApplication+FIRForegroundWindowScene.h"
21+
22+
@implementation UIApplication (FIRForegroundWindowScene)
23+
24+
- (nullable UIWindowScene *)fir_foregroundWindowScene {
25+
for (UIScene *connectedScene in [UIApplication sharedApplication].connectedScenes) {
26+
// Direct check for UIWindowScene class is required to avoid return an instance of another
27+
// UIScene subclass. It may be an instance of CPTemplateApplicationScene or
28+
// CPTemplateApplicationDashboardScene in case of CarPlay. This check fixes the following crash:
29+
// https://github.com/firebase/firebase-ios-sdk/issues/9376
30+
if ([connectedScene isKindOfClass:[UIWindowScene class]] &&
31+
connectedScene.activationState == UISceneActivationStateForegroundActive) {
32+
return (UIWindowScene *)connectedScene;
33+
}
34+
}
35+
return nil;
36+
}
37+
38+
@end
39+
40+
#endif // TARGET_OS_IOS || TARGET_OS_TV

0 commit comments

Comments
 (0)