From c23ce015bec7a768c34680fab15cc14694dd107e Mon Sep 17 00:00:00 2001 From: ghenry22 Date: Wed, 8 Nov 2017 14:20:55 +0800 Subject: [PATCH 1/2] attach wkwebview to keywindow so it stays active even when app is in background --- src/ios/CDVWKWebViewEngine.m | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ios/CDVWKWebViewEngine.m b/src/ios/CDVWKWebViewEngine.m index 43172ed4..c267a499 100644 --- a/src/ios/CDVWKWebViewEngine.m +++ b/src/ios/CDVWKWebViewEngine.m @@ -122,6 +122,9 @@ - (instancetype)initWithFrame:(CGRect)frame if(!IsAtLeastiOSVersion(@"9.0")) { return nil; } + // add to keyWindow to ensure it is 'active' + [UIApplication.sharedApplication.keyWindow addSubview:self.engineWebView]; + self.frame = frame; [GCDWebServer setLogLevel: kGCDWebServerLoggingLevel_Warning]; self.webServer = [[GCDWebServer alloc] init]; @@ -205,6 +208,8 @@ - (void)pluginInitialize configuration.userContentController = userContentController; // re-create WKWebView, since we need to update configuration + // remove from keyWindow before recreating + [self.engineWebView removeFromSuperview]; WKWebView* wkWebView = [[WKWebView alloc] initWithFrame:self.frame configuration:configuration]; #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 @@ -216,6 +221,9 @@ - (void)pluginInitialize wkWebView.UIDelegate = self.uiDelegate; self.engineWebView = wkWebView; + // add to keyWindow to ensure it is 'active' + [UIApplication.sharedApplication.keyWindow addSubview:self.engineWebView]; + if (IsAtLeastiOSVersion(@"9.0") && [self.viewController isKindOfClass:[CDVViewController class]]) { wkWebView.customUserAgent = ((CDVViewController*) self.viewController).userAgent; } From f45ba4ba3511c06116ce4d7801e84a90f39fe677 Mon Sep 17 00:00:00 2001 From: ghenry22 Date: Wed, 15 Nov 2017 14:05:28 +0800 Subject: [PATCH 2/2] add _alwaysRunsAtForegroundPriority setting tie background operation to config.xml WKEnableBackground setting. Default background mode to NO which will immediately suspend GCDwebserver and allow wkwebview to be eagerly suspended if the OS wants to. --- src/ios/CDVWKWebViewEngine.m | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/ios/CDVWKWebViewEngine.m b/src/ios/CDVWKWebViewEngine.m index c267a499..e6e35f1e 100644 --- a/src/ios/CDVWKWebViewEngine.m +++ b/src/ios/CDVWKWebViewEngine.m @@ -105,6 +105,13 @@ @interface CDVWKWebViewEngine () @end +// expose private configuration value required for background operation +@interface WKWebViewConfiguration () + +@property (setter=_setAlwaysRunsAtForegroundPriority:, nonatomic) bool _alwaysRunsAtForegroundPriority; + +@end + // see forwardingTargetForSelector: selector comment for the reason for this pragma #pragma clang diagnostic ignored "-Wprotocol" @@ -157,6 +164,8 @@ - (WKWebViewConfiguration*) createConfigurationFromSettings:(NSDictionary*)setti if (settings == nil) { return configuration; } + //required to stop wkwebview suspending in background too eagerly (as used in background mode plugin) + configuration._alwaysRunsAtForegroundPriority = [settings cordovaBoolSettingForKey:@"WKEnableBackground" defaultValue:NO]; configuration.allowsInlineMediaPlayback = [settings cordovaBoolSettingForKey:@"AllowInlineMediaPlayback" defaultValue:YES]; configuration.suppressesIncrementalRendering = [settings cordovaBoolSettingForKey:@"SuppressesIncrementalRendering" defaultValue:NO]; configuration.allowsAirPlayForMediaPlayback = [settings cordovaBoolSettingForKey:@"MediaPlaybackAllowsAirPlay" defaultValue:YES];