diff --git a/ios/RNSimpleToast.mm b/ios/RNSimpleToast.mm index f31c885..067c44f 100644 --- a/ios/RNSimpleToast.mm +++ b/ios/RNSimpleToast.mm @@ -1,9 +1,9 @@ #import "RNSimpleToast.h" #import "UIView+Toast.h" +#import "RNToastViewController.h" #import #import "RNToastView.h" -#import "RNToastWindow.h" static double defaultPositionId = 2.0; @@ -106,17 +106,18 @@ - (void)_show:(NSString *)msg NSString *positionString = RNToastPositionMap[@(position)] ?: CSToastPositionBottom; dispatch_async(dispatch_get_main_queue(), ^{ - UIWindow *window = [RNToastWindow new]; + RNToastViewController *controller = [RNToastViewController new]; + [controller show]; BOOL kbdAvoidEnabled = [CSToastPositionBottom isEqualToString:positionString]; - UIView *view = [[RNToastView alloc] initWithFrame:window.bounds kbdHeight:self->_kbdHeight kbdAvoidEnabled:kbdAvoidEnabled]; - [window addSubview:view]; + UIView *view = [[RNToastView alloc] initWithFrame:controller.toastWindow.bounds kbdHeight:self->_kbdHeight kbdAvoidEnabled:kbdAvoidEnabled]; + [controller.toastWindow addSubview:view]; UIView __weak *weakView = view; UIView *toast = [view toastViewForMessage:msg title:nil image:nil style:style]; void (^completion)(BOOL) = ^(BOOL didTap) { [weakView removeFromSuperview]; - [window setHidden:YES]; + [controller hide]; }; // CSToastManager state is shared among toasts, and is used when toast is shown // so modifications to it should happen in the dispatch_get_main_queue block diff --git a/ios/RNToastViewController.h b/ios/RNToastViewController.h new file mode 100644 index 0000000..6885934 --- /dev/null +++ b/ios/RNToastViewController.h @@ -0,0 +1,10 @@ +#import + +@interface RNToastViewController : NSObject + +@property(nonatomic, strong) UIWindow *toastWindow; + +- (void)show; +- (void)hide; + +@end diff --git a/ios/RNToastViewController.m b/ios/RNToastViewController.m new file mode 100644 index 0000000..72439f1 --- /dev/null +++ b/ios/RNToastViewController.m @@ -0,0 +1,54 @@ + +#import "RNToastViewController.h" +#import +#import "RNToastWindow.h" + +@implementation RNToastViewController + +- (UIWindow *)toastWindow +{ + if (_toastWindow == nil) { + _toastWindow = [self getUIWindowFromScene]; + + if (_toastWindow == nil) { + UIWindow *keyWindow = RCTSharedApplication().keyWindow; + if (keyWindow) { + _toastWindow = [[RNToastWindow alloc] initWithFrame:keyWindow.bounds]; + } else { + // keyWindow is nil, so we cannot create and initialize _toastWindow + NSLog(@"Unable to create alert window: keyWindow is nil"); + } + } + } + + return _toastWindow; +} + +- (void)show { + [self.toastWindow setHidden:NO]; +} + +- (void)hide { + [_toastWindow setHidden:YES]; + + if (@available(iOS 13, *)) { + _toastWindow.windowScene = nil; + } + + _toastWindow = nil; +} + +- (UIWindow *)getUIWindowFromScene +{ + if (@available(iOS 13.0, *)) { + for (UIScene *scene in RCTSharedApplication().connectedScenes) { + if (scene.activationState == UISceneActivationStateForegroundActive && + [scene isKindOfClass:[UIWindowScene class]]) { + return [[RNToastWindow alloc] initWithWindowScene:(UIWindowScene *)scene]; + } + } + } + return nil; +} + +@end diff --git a/ios/RNToastWindow.m b/ios/RNToastWindow.m index 55a0891..1b5484d 100644 --- a/ios/RNToastWindow.m +++ b/ios/RNToastWindow.m @@ -1,29 +1,16 @@ #import "RNToastWindow.h" -#import @implementation RNToastWindow -- (instancetype)init -{ - if (@available(iOS 13.0, *)) { - for (UIScene *scene in RCTSharedApplication().connectedScenes) { - if (scene.activationState == UISceneActivationStateForegroundActive && - [scene isKindOfClass:[UIWindowScene class]]) { - self = [super initWithWindowScene:(UIWindowScene *)scene]; - } - } - } - if (!self) { - UIWindow *keyWindow = RCTSharedApplication().keyWindow; - if (keyWindow) { - self = [super initWithFrame:keyWindow.bounds]; - } else { - // keyWindow is nil, so we cannot create and initialize _toastWindow - NSLog(@"Unable to create alert window: keyWindow is nil"); - } +- (instancetype)initWithFrame:(CGRect)frame { + if (self = [super initWithFrame:frame]) { + self.windowLevel = UIWindowLevelAlert + 1; } - if (self) { - [self setHidden:NO]; + return self; +} + +- (instancetype)initWithWindowScene:(UIWindowScene *)windowScene { + if (self = [super initWithWindowScene:windowScene]) { self.windowLevel = UIWindowLevelAlert + 1; } return self; @@ -35,11 +22,4 @@ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { else return hitView; } -- (void)dealloc -{ - if (@available(iOS 13, *)) { - self.windowScene = nil; - } -} - @end