/
RNSplashScreen.m
73 lines (61 loc) · 1.93 KB
/
RNSplashScreen.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
/**
* SplashScreen
* 启动屏
* from:http://www.devio.org
* Author:CrazyCodeBoy
* GitHub:https://github.com/crazycodeboy
* Email:crazycodeboy@gmail.com
*/
#import "RNSplashScreen.h"
#import <React/RCTBridge.h>
static bool waiting = true;
static bool addedJsLoadErrorObserver = false;
static UIView* loadingView = nil;
@implementation RNSplashScreen
- (dispatch_queue_t)methodQueue{
return dispatch_get_main_queue();
}
RCT_EXPORT_MODULE(SplashScreen)
+ (void)show {
if (!addedJsLoadErrorObserver) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jsLoadError:) name:RCTJavaScriptDidFailToLoadNotification object:nil];
addedJsLoadErrorObserver = true;
}
while (waiting) {
NSDate* later = [NSDate dateWithTimeIntervalSinceNow:0.1];
[[NSRunLoop mainRunLoop] runUntilDate:later];
}
}
+ (void)showSplash:(NSString*)splashScreen inRootView:(UIView*)rootView {
if (!loadingView) {
loadingView = [[[NSBundle mainBundle] loadNibNamed:splashScreen owner:self options:nil] objectAtIndex:0];
CGRect frame = rootView.frame;
frame.origin = CGPointMake(0, 0);
loadingView.frame = frame;
}
waiting = false;
[rootView addSubview:loadingView];
}
+ (void)hide {
if (waiting) {
dispatch_async(dispatch_get_main_queue(), ^{
waiting = false;
});
} else {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[loadingView removeFromSuperview];
});
}
}
+ (void) jsLoadError:(NSNotification*)notification
{
// If there was an error loading javascript, hide the splash screen so it can be shown. Otherwise the splash screen will remain forever, which is a hassle to debug.
[RNSplashScreen hide];
}
RCT_EXPORT_METHOD(hide) {
[RNSplashScreen hide];
}
RCT_EXPORT_METHOD(show) {
[RNSplashScreen show];
}
@end