-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathRCTDevSupport+UIScene.m
181 lines (153 loc) · 6.4 KB
/
RCTDevSupport+UIScene.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
#import <React/RCTDevLoadingView.h>
#import <React/RCTJSStackFrame.h>
#import <React/RCTUtils.h>
#import <React/RCTVersion.h>
#import "React+Compatibility.h"
#if DEBUG
// MARK: - RCTRedBoxWindow
// MARK: - [0.64] `RCTRedBox` doesn't appear in apps implementing `UISceneDelegate`
// https://github.com/facebook/react-native/commit/46c77dc296dfab754356cd9346a01dae8d4869f4
#if REACT_NATIVE_VERSION < 6400
// MARK: - [0.63] `RCTRedBox` doesn't appear in apps implementing `UISceneDelegate`
// https://github.com/facebook/react-native/commit/d0a32c2011ca00991be45ac3fa320f4fc663b2e8
@protocol RCTRedBoxWindowActionDelegate;
@interface RCTRedBoxWindow : UIWindow <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIViewController *rootViewController;
@property (nonatomic, weak) id<RCTRedBoxWindowActionDelegate> actionDelegate;
- (void)dismiss;
// showErrorMessage:withStack:isUpdate: was renamed showErrorMessage:withStack:isUpdate:errorCookie:
// https://github.com/facebook/react-native/commit/850a8352c91f10842d84c3e811cf6e2a0927f349
#if REACT_NATIVE_VERSION < 6200
- (void)showErrorMessage:(NSString *)message
withStack:(NSArray<RCTJSStackFrame *> *)stack
isUpdate:(BOOL)isUpdate;
#else
- (void)showErrorMessage:(NSString *)message
withStack:(NSArray<RCTJSStackFrame *> *)stack
isUpdate:(BOOL)isUpdate
errorCookie:(int)errorCookie;
#endif
@end
@implementation RCTRedBoxWindow (UISceneSupport)
#if REACT_NATIVE_VERSION < 6200
static void (*orig_showErrorMessage)(id, SEL, NSString *, NSArray<RCTJSStackFrame *> *, BOOL);
#else
static void (*orig_showErrorMessage)(id, SEL, NSString *, NSArray<RCTJSStackFrame *> *, BOOL, int);
#endif
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
#if REACT_NATIVE_VERSION < 6200
orig_showErrorMessage =
(void (*)(id, SEL, NSString *, NSArray<RCTJSStackFrame *> *, BOOL))RTASwizzleSelector(
class,
@selector(showErrorMessage:withStack:isUpdate:),
@selector(rta_showErrorMessage:withStack:isUpdate:));
#else
orig_showErrorMessage =
(void (*)(id, SEL, NSString *, NSArray<RCTJSStackFrame *> *, BOOL, int))
RTASwizzleSelector(class,
@selector(showErrorMessage:withStack:isUpdate:errorCookie:),
@selector(rta_showErrorMessage:withStack:isUpdate:errorCookie:));
#endif // REACT_NATIVE_VERSION < 6200
#if REACT_NATIVE_VERSION < 6300
RTASwizzleSelector(class, @selector(dismiss), @selector(rta_dismiss));
RTASwizzleSelector(class, @selector(makeKeyAndVisible), @selector(rta_makeKeyAndVisible));
#endif // REACT_NATIVE_VERSION < 6300
});
}
#if REACT_NATIVE_VERSION < 6300
- (void)rta_makeKeyAndVisible
{
// Manually adding the rootViewController's view to the view hierarchy is no
// longer supported. Overriding to allow UIWindow to add the
// rootViewController's view to the view hierarchy itself.
}
- (void)rta_dismiss
{
[self.rootViewController dismissViewControllerAnimated:YES completion:nil];
}
#endif // REACT_NATIVE_VERSION < 6300
#if REACT_NATIVE_VERSION < 6200
- (void)rta_showErrorMessage:(NSString *)message
withStack:(NSArray<RCTJSStackFrame *> *)stack
isUpdate:(BOOL)isUpdate
{
if (self.rootViewController.presentingViewController == nil) {
self.rootViewController.view.backgroundColor = UIColor.blackColor;
[RCTSharedApplication().delegate.window.rootViewController
presentViewController:self.rootViewController
animated:NO
completion:nil];
orig_showErrorMessage(
self, @selector(showErrorMessage:withStack:isUpdate:), message, stack, isUpdate);
}
}
#else
- (void)rta_showErrorMessage:(NSString *)message
withStack:(NSArray<RCTJSStackFrame *> *)stack
isUpdate:(BOOL)isUpdate
errorCookie:(int)errorCookie
{
#if REACT_NATIVE_VERSION < 6300
if (self.rootViewController.presentingViewController == nil) {
self.rootViewController.view.backgroundColor = UIColor.blackColor;
[RCTSharedApplication().delegate.window.rootViewController
presentViewController:self.rootViewController
animated:NO
completion:nil];
}
#endif // REACT_NATIVE_VERSION < 6300
orig_showErrorMessage(self,
@selector(showErrorMessage:withStack:isUpdate:),
message,
stack,
isUpdate && self.rootViewController.presentingViewController != nil,
errorCookie);
}
#endif // REACT_NATIVE_VERSION < 6200
@end
#endif // REACT_NATIVE_VERSION < 6400
// MARK: - [0.63] RCTDevLoadingView doesn't show up with UIScene
// https://github.com/facebook/react-native/commit/74b667dbc2a48183dec0b9c3b5401bc3f9e54e7b
#if REACT_NATIVE_VERSION < 6300
@implementation RCTDevLoadingView (UISceneSupport)
static void (*orig_showMessage)(id, SEL, NSString *, UIColor *, UIColor *);
+ (void)initialize
{
if ([self class] != [RCTDevLoadingView class]) {
return;
}
if (@available(iOS 13.0, *)) {
orig_showMessage = (void (*)(id, SEL, NSString *, UIColor *, UIColor *))RTASwizzleSelector(
[self class],
@selector(showMessage:color:backgroundColor:),
@selector(rta_showMessage:color:backgroundColor:));
}
}
- (void)rta_showMessage:(NSString *)message
color:(UIColor *)color
backgroundColor:(UIColor *)backgroundColor
{
orig_showMessage(
self, @selector(showMessage:color:backgroundColor:), message, color, backgroundColor);
if (@available(iOS 13.0, *)) {
dispatch_async(dispatch_get_main_queue(), ^{
Ivar _window = class_getInstanceVariable([self class], [@"_window" UTF8String]);
id window = object_getIvar(self, _window);
if ([window isKindOfClass:[UIWindow class]]) {
UIWindowScene *scene =
(UIWindowScene *)UIApplication.sharedApplication.connectedScenes.anyObject;
[window setWindowScene:scene];
}
});
}
}
@end
#endif // REACT_NATIVE_VERSION < 6300
#endif // DEBUG