Skip to content

Commit 124984b

Browse files
committed
fix: Add preference for crash recovery behaviour
This adds a `CrashRecoveryBehavior` preference with options `"refresh"` (default - try to refresh the WebView URL) and `"reload"` (load the starting page again in the WebView). This doesn't solve the root issue where sometimes refreshing the WebView still results in a blank screen, but gives people the option to force a full reload (which seems to be what people are using as a workaround for the blank screen bug). Closes apacheGH-1232. Closes apacheGH-1235. Closes apacheGH-1355. Closes apacheGH-1393. Closes apacheGH-1533. Closes apacheGH-1552.
1 parent 6b39d0e commit 124984b

File tree

4 files changed

+44
-62
lines changed

4 files changed

+44
-62
lines changed

CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,6 @@ - (void)pluginInitialize
266266

267267
[self updateSettings:settings];
268268

269-
// check if content thread has died on resume
270-
NSLog(@"%@", @"CDVWebViewEngine will reload WKWebView if required on resume");
271-
[[NSNotificationCenter defaultCenter]
272-
addObserver:self
273-
selector:@selector(onAppWillEnterForeground:)
274-
name:UIApplicationWillEnterForegroundNotification object:nil];
275-
276269
NSLog(@"Using WKWebView");
277270
}
278271

@@ -285,37 +278,6 @@ - (void)dispose
285278
[super dispose];
286279
}
287280

288-
- (void) onAppWillEnterForeground:(NSNotification*)notification {
289-
if ([self shouldReloadWebView]) {
290-
NSLog(@"%@", @"CDVWebViewEngine reloading!");
291-
[(WKWebView*)_engineWebView reload];
292-
}
293-
}
294-
295-
- (BOOL)shouldReloadWebView
296-
{
297-
WKWebView* wkWebView = (WKWebView*)_engineWebView;
298-
return [self shouldReloadWebView:wkWebView.URL title:wkWebView.title];
299-
}
300-
301-
- (BOOL)shouldReloadWebView:(NSURL*)location title:(NSString*)title
302-
{
303-
BOOL title_is_nil = (title == nil);
304-
BOOL location_is_blank = [[location absoluteString] isEqualToString:@"about:blank"];
305-
306-
BOOL reload = (title_is_nil || location_is_blank);
307-
308-
#ifdef DEBUG
309-
NSLog(@"%@", @"CDVWebViewEngine shouldReloadWebView::");
310-
NSLog(@"CDVWebViewEngine shouldReloadWebView title: %@", title);
311-
NSLog(@"CDVWebViewEngine shouldReloadWebView location: %@", [location absoluteString]);
312-
NSLog(@"CDVWebViewEngine shouldReloadWebView reload: %u", reload);
313-
#endif
314-
315-
return reload;
316-
}
317-
318-
319281
- (id)loadRequest:(NSURLRequest*)request
320282
{
321283
if ([self canLoadRequest:request]) { // can load, differentiate between file urls and other schemes
@@ -566,7 +528,14 @@ - (void)webView:(WKWebView*)theWebView didFailNavigation:(WKNavigation*)navigati
566528

567529
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView
568530
{
569-
[webView reload];
531+
CDVSettingsDictionary *settings = self.commandDelegate.settings;
532+
NSString *recoveryBehavior = [settings cordovaSettingForKey:@"CrashRecoveryBehavior"];
533+
534+
if ([recoveryBehavior isEqualToString:@"reload"]) {
535+
[self.viewController loadStartPage];
536+
} else {
537+
[webView reload];
538+
}
570539
}
571540

572541
- (BOOL)defaultResourcePolicyForURL:(NSURL*)url

CordovaLib/Classes/Public/CDVViewController.m

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -382,27 +382,7 @@ - (void)viewDidLoad
382382
[CDVTimer stop:@"TotalPluginStartup"];
383383
}
384384

385-
// /////////////////
386-
NSURL* appURL = [self appUrl];
387-
388-
if (appURL) {
389-
NSURLRequest* appReq = [NSURLRequest requestWithURL:appURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
390-
[_webViewEngine loadRequest:appReq];
391-
} else {
392-
NSString* loadErr = [NSString stringWithFormat:@"ERROR: Start Page at '%@/%@' was not found.", self.webContentFolderName, self.startPage];
393-
NSLog(@"%@", loadErr);
394-
395-
NSURL* errorUrl = [self errorURL];
396-
if (errorUrl) {
397-
errorUrl = [NSURL URLWithString:[NSString stringWithFormat:@"?error=%@", [loadErr stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLPathAllowedCharacterSet]] relativeToURL:errorUrl];
398-
NSLog(@"%@", [errorUrl absoluteString]);
399-
[_webViewEngine loadRequest:[NSURLRequest requestWithURL:errorUrl]];
400-
} else {
401-
NSString* html = [NSString stringWithFormat:@"<html><body> %@ </body></html>", loadErr];
402-
[_webViewEngine loadHTMLString:html baseURL:nil];
403-
}
404-
}
405-
// /////////////////
385+
[self loadStartPage];
406386

407387
[self.webView setBackgroundColor:self.backgroundColor];
408388
[self.launchView setBackgroundColor:self.splashBackgroundColor];
@@ -765,6 +745,29 @@ - (void)createStatusBarView
765745
self.statusBar.hidden = YES;
766746
}
767747

748+
- (void)loadStartPage
749+
{
750+
NSURL *appURL = [self appUrl];
751+
752+
if (appURL) {
753+
NSURLRequest *appReq = [NSURLRequest requestWithURL:appURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
754+
[_webViewEngine loadRequest:appReq];
755+
} else {
756+
NSString *loadErr = [NSString stringWithFormat:@"ERROR: Start Page at '%@/%@' was not found.", self.webContentFolderName, self.startPage];
757+
NSLog(@"%@", loadErr);
758+
759+
NSURL *errorUrl = [self errorURL];
760+
if (errorUrl) {
761+
errorUrl = [NSURL URLWithString:[NSString stringWithFormat:@"?error=%@", [loadErr stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLPathAllowedCharacterSet]] relativeToURL:errorUrl];
762+
NSLog(@"%@", [errorUrl absoluteString]);
763+
[_webViewEngine loadRequest:[NSURLRequest requestWithURL:errorUrl]];
764+
} else {
765+
NSString *html = [NSString stringWithFormat:@"<html><body> %@ </body></html>", loadErr];
766+
[_webViewEngine loadHTMLString:html baseURL:nil];
767+
}
768+
}
769+
}
770+
768771
#pragma mark CordovaCommands
769772

770773
- (void)registerPlugin:(CDVPlugin*)plugin withClassName:(NSString*)className
@@ -846,8 +849,7 @@ - (bool)checkAndReinitViewUrl
846849
{
847850
NSURL* appURL = [self appUrl];
848851
if ([self isUrlEmpty: [_webViewEngine URL]] && ![self isUrlEmpty: appURL]) {
849-
NSURLRequest* appReq = [NSURLRequest requestWithURL:appURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
850-
[_webViewEngine loadRequest:appReq];
852+
[self loadStartPage];
851853
return true;
852854
}
853855
return false;

CordovaLib/CordovaLib.docc/upgrading-8.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,5 @@ The following headers are deprecated due to adding global category extensions to
345345

346346
* The ``CDVViewController/showLaunchScreen:`` method is deprecated.
347347
This method has been renamed to ``CDVViewController/showSplashScreen:``.
348+
349+
* Added a new ``CDVViewController/loadStartPage`` method to load the initial starting page in the web view, replacing any existing content.

CordovaLib/include/Cordova/CDVViewController.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,15 @@ NS_ASSUME_NONNULL_BEGIN
263263
*/
264264
- (UIView*)newCordovaViewWithFrame:(CGRect)bounds;
265265

266+
/**
267+
Loads the starting page in the web view, replacing any existing content.
268+
269+
@Metadata {
270+
@Available(Cordova, introduced: "8.0.0")
271+
}
272+
*/
273+
- (void)loadStartPage;
274+
266275
/**
267276
Returns the ``CDVPlugin`` instance of the given plugin name, creating the
268277
instance if one does not exist.

0 commit comments

Comments
 (0)