Skip to content

Commit

Permalink
Merge pull request crazycodeboy#33 from Vydia/hide-screen-when-js-fails
Browse files Browse the repository at this point in the history
Hide screen when a js loading error occurs
  • Loading branch information
crazycodeboy authored May 31, 2017
2 parents 6adbc4c + cc6b759 commit 2cfa070
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ios/SplashScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* Email:[email protected]
*/
#import <React/RCTBridgeModule.h>
//#import "RCTBridgeModule.h"

@interface SplashScreen : NSObject<RCTBridgeModule>
+ (void)show;
+ (void)hide;
@end
20 changes: 19 additions & 1 deletion ios/SplashScreen.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
*/

#import "SplashScreen.h"
#import <React/RCTBridge.h>

static bool waiting = true;
static bool addedJsLoadErrorObserver = false;

@implementation SplashScreen
- (dispatch_queue_t)methodQueue{
Expand All @@ -18,16 +20,32 @@ - (dispatch_queue_t)methodQueue{
RCT_EXPORT_MODULE()

+ (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];
}
}

RCT_EXPORT_METHOD(hide) {
+ (void)hide {
dispatch_async(dispatch_get_main_queue(),
^{
waiting = false;
});
}

+ (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.
[SplashScreen hide];
}

RCT_EXPORT_METHOD(hide) {
[SplashScreen hide];
}

@end
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-splash-screen",
"version": "2.0.0",
"version": "2.1.0",
"description": "A splash screen for react-native, hide when application loaded ,it works on iOS and Android.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 2cfa070

Please sign in to comment.