Skip to content

Commit 2cfa070

Browse files
authored
Merge pull request #33 from Vydia/hide-screen-when-js-fails
Hide screen when a js loading error occurs
2 parents 6adbc4c + cc6b759 commit 2cfa070

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

ios/SplashScreen.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
88
*/
99
#import <React/RCTBridgeModule.h>
10-
//#import "RCTBridgeModule.h"
1110

1211
@interface SplashScreen : NSObject<RCTBridgeModule>
1312
+ (void)show;
13+
+ (void)hide;
1414
@end

ios/SplashScreen.m

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
*/
99

1010
#import "SplashScreen.h"
11+
#import <React/RCTBridge.h>
1112

1213
static bool waiting = true;
14+
static bool addedJsLoadErrorObserver = false;
1315

1416
@implementation SplashScreen
1517
- (dispatch_queue_t)methodQueue{
@@ -18,16 +20,32 @@ - (dispatch_queue_t)methodQueue{
1820
RCT_EXPORT_MODULE()
1921

2022
+ (void)show {
23+
if (!addedJsLoadErrorObserver) {
24+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jsLoadError:) name:RCTJavaScriptDidFailToLoadNotification object:nil];
25+
addedJsLoadErrorObserver = true;
26+
}
27+
2128
while (waiting) {
2229
NSDate* later = [NSDate dateWithTimeIntervalSinceNow:0.1];
2330
[[NSRunLoop mainRunLoop] runUntilDate:later];
2431
}
2532
}
2633

27-
RCT_EXPORT_METHOD(hide) {
34+
+ (void)hide {
2835
dispatch_async(dispatch_get_main_queue(),
2936
^{
3037
waiting = false;
3138
});
3239
}
40+
41+
+ (void) jsLoadError:(NSNotification*)notification
42+
{
43+
// 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.
44+
[SplashScreen hide];
45+
}
46+
47+
RCT_EXPORT_METHOD(hide) {
48+
[SplashScreen hide];
49+
}
50+
3351
@end

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-splash-screen",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "A splash screen for react-native, hide when application loaded ,it works on iOS and Android.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)