@@ -107,6 +107,7 @@ @interface CDVWKWebViewEngine ()
107
107
@property (nonatomic , readwrite ) CGRect frame;
108
108
@property (nonatomic , strong ) NSString *userAgentCreds;
109
109
@property (nonatomic , assign ) BOOL internalConnectionsOnly;
110
+
110
111
@property (nonatomic , readwrite ) NSString *CDV_LOCAL_SERVER;
111
112
@end
112
113
@@ -353,6 +354,14 @@ - (void)pluginInitialize
353
354
addObserver: self
354
355
selector: @selector (onAppWillEnterForeground: )
355
356
name: UIApplicationWillEnterForegroundNotification object: nil ];
357
+ [[NSNotificationCenter defaultCenter ]
358
+ addObserver: self
359
+ selector: @selector (onSocketError: )
360
+ name: @" socketUnknownError" object: nil ];
361
+ [[NSNotificationCenter defaultCenter ]
362
+ addObserver: self
363
+ selector: @selector (onSocketError: )
364
+ name: @" socketInUseError" object: nil ];
356
365
357
366
NSLog (@" Using Ionic WKWebView" );
358
367
@@ -417,7 +426,11 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
417
426
if (context == KVOContext) {
418
427
if (object == [self webView ] && [keyPath isEqualToString: @" URL" ] && [object valueForKeyPath: keyPath] == nil ){
419
428
NSLog (@" URL is nil. Reloading WKWebView" );
420
- [(WKWebView *)_engineWebView reload ];
429
+ if ([self .webServer isRunning ]) {
430
+ [(WKWebView *)_engineWebView reload ];
431
+ } else {
432
+ [self loadErrorPage: nil ];
433
+ }
421
434
}
422
435
} else {
423
436
[super observeValueForKeyPath: keyPath ofObject: object change: change context: context];
@@ -426,11 +439,19 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
426
439
427
440
- (void )onAppWillEnterForeground : (NSNotification *)notification {
428
441
if ([self shouldReloadWebView ]) {
429
- NSLog (@" %@ " , @" CDVWKWebViewEngine reloading!" );
430
- [(WKWebView *)_engineWebView reload ];
442
+ if ([self .webServer isRunning ]) {
443
+ NSLog (@" %@ " , @" CDVWKWebViewEngine reloading!" );
444
+ [(WKWebView *)_engineWebView reload ];
445
+ } else {
446
+ [self loadErrorPage: nil ];
447
+ }
431
448
}
432
449
}
433
450
451
+ - (void )onSocketError : (NSNotification *)notification {
452
+ [self loadErrorPage: nil ];
453
+ }
454
+
434
455
- (BOOL )shouldReloadWebView
435
456
{
436
457
WKWebView * wkWebView = (WKWebView *)_engineWebView;
@@ -475,17 +496,25 @@ - (id)loadRequest:(NSURLRequest *)request
475
496
if ([self .webServer isRunning ]) {
476
497
return [(WKWebView *)_engineWebView loadRequest: request];
477
498
} else {
478
- NSString * errorHtml = [NSString stringWithFormat:
479
- @" <html>"
480
- @" <head><title>Error</title></head>"
481
- @" <div style='font-size:2em'>"
482
- @" <p>The App Server is not running.</p>"
483
- @" <p>Close other apps and try again.</p>"
484
- @" </div>"
485
- @" </html>"
486
- ];
487
- return [self loadHTMLString: errorHtml baseURL: request.URL];
499
+ return [self loadErrorPage: request];
500
+ }
501
+ }
502
+
503
+ - (id )loadErrorPage : (NSURLRequest *)request
504
+ {
505
+ if (!request) {
506
+ request = [NSURLRequest requestWithURL: [NSURL URLWithString: self .CDV_LOCAL_SERVER]];
488
507
}
508
+ NSString * errorHtml = [NSString stringWithFormat:
509
+ @" <html>"
510
+ @" <head><title>Error</title></head>"
511
+ @" <div style='font-size:2em'>"
512
+ @" <p><b>Error</b></p>"
513
+ @" <p>Unable to load app.</p>"
514
+ @" </div>"
515
+ @" </html>"
516
+ ];
517
+ return [self loadHTMLString: errorHtml baseURL: request.URL];
489
518
}
490
519
491
520
- (id )loadHTMLString : (NSString *)string baseURL : (NSURL *)baseURL
@@ -744,7 +773,11 @@ - (void)webView:(WKWebView*)theWebView didFailNavigation:(WKNavigation*)navigati
744
773
745
774
- (void )webViewWebContentProcessDidTerminate : (WKWebView *)webView
746
775
{
747
- [webView reload ];
776
+ if ([self .webServer isRunning ]) {
777
+ [webView reload ];
778
+ } else {
779
+ [self loadErrorPage: nil ];
780
+ }
748
781
}
749
782
750
783
- (BOOL )defaultResourcePolicyForURL : (NSURL *)url
@@ -815,14 +848,19 @@ - (void) webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigat
815
848
816
849
-(void )getServerBasePath : (CDVInvokedUrlCommand*)command
817
850
{
818
- [self .commandDelegate sendPluginResult: [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString: self .basePath] callbackId: command.callbackId];
851
+ [self .commandDelegate sendPluginResult: [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString: self .basePath] callbackId: command.callbackId];
819
852
}
820
853
821
854
-(void )setServerBasePath : (CDVInvokedUrlCommand*)command
822
855
{
823
- NSString * path = [command argumentAtIndex: 0 ];
824
- [self setServerPath: path];
825
- [(WKWebView *)_engineWebView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: self .CDV_LOCAL_SERVER]]];
856
+ NSString * path = [command argumentAtIndex: 0 ];
857
+ [self setServerPath: path];
858
+ NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: self .CDV_LOCAL_SERVER]];
859
+ if ([self .webServer isRunning ]) {
860
+ [(WKWebView *)_engineWebView loadRequest: request];
861
+ } else {
862
+ [self loadErrorPage: request];
863
+ }
826
864
}
827
865
828
866
-(void )setServerPath : (NSString *) path
0 commit comments