Skip to content
34 changes: 30 additions & 4 deletions Firebase/InAppMessaging/Flows/FIRIAMDisplayExecutor.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#import <FirebaseCore/FIRLogger.h>
#import <UIKit/UIKit.h>

#import "FIRCore+InAppMessaging.h"
#import "FIRIAMActivityLogger.h"
Expand All @@ -41,6 +42,8 @@ @interface FIRIAMDisplayExecutor () <FIRInAppMessagingDisplayDelegate>
@property(nonatomic) BOOL impressionRecorded;
@property(nonatomic, nonnull, readonly) id<FIRIAMAnalyticsEventLogger> analyticsEventLogger;
@property(nonatomic, nonnull, readonly) FIRIAMActionURLFollower *actionURLFollower;
// Used for displaying the test on device message error alert.
@property(nonatomic, strong) UIWindow *alertWindow;
@end

@implementation FIRIAMDisplayExecutor {
Expand Down Expand Up @@ -307,14 +310,37 @@ - (void)displayMessageLoadError:(NSError *)error {

UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action){
handler:^(UIAlertAction *action) {
self.alertWindow.hidden = NO;
self.alertWindow = nil;
}];

[alert addAction:defaultAction];

[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert
animated:YES
completion:nil];
dispatch_async(dispatch_get_main_queue(), ^{
#if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
if (@available(iOS 13.0, *)) {
UIWindowScene *foregroundedScene = nil;
for (UIWindowScene *connectedScene in [UIApplication sharedApplication].connectedScenes) {
if (connectedScene.activationState == UISceneActivationStateForegroundActive) {
foregroundedScene = connectedScene;
break;
}
}

if (foregroundedScene == nil) {
return;
}
self.alertWindow = [[UIWindow alloc] initWithWindowScene:foregroundedScene];
}
#else // defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
self.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
#endif
UIViewController *alertViewController = [[UIViewController alloc] init];
self.alertWindow.rootViewController = alertViewController;
self.alertWindow.hidden = NO;
[alertViewController presentViewController:alert animated:YES completion:nil];
});
}

- (instancetype)initWithInAppMessaging:(FIRInAppMessaging *)inAppMessaging
Expand Down