cordova-plugin-background-mode
cordova-plugin-background-mode copied to clipboard
iOS 12.x not working - workaround
The current version of the plugin are working well in iOS 14.4 but in iOS 12.x (in physical device because in emulator works well) the javascript execution was stopped automatically when the app was sended to the background.
It seems to be a bug with WKWebview. The following workaround works for me, at least to all proof that I have been able to do.
Here the workaround:
-
In
config.xml add
<preference name="WKSuspendInBackground" value="false" />
-
In
cordova/platforms/ios/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m
(line 80 - createConfigurationFromSettings)
if(![settings cordovaBoolSettingForKey:@"WKSuspendInBackground" defaultValue:YES]){
NSString* _BGStatus;
if (@available(iOS 12.2, *)) {
// do stuff for iOS 12.2 and newer
NSLog(@"iOS 12.2+ detected");
NSString* str = @"YWx3YXlzUnVuc0F0Rm9yZWdyb3VuZFByaW9yaXR5";
NSData* data = [[NSData alloc] initWithBase64EncodedString:str options:0];
_BGStatus = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
} else {
// do stuff for iOS 12.1 and older
NSLog(@"iOS Below 12.2 detected");
NSString* str = @"X2Fsd2F5c1J1bnNBdEZvcmVncm91bmRQcmlvcml0eQ==";
NSData* data = [[NSData alloc] initWithBase64EncodedString:str options:0];
_BGStatus = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
[configuration setValue:[NSNumber numberWithBool:YES]
forKey:_BGStatus];
}
Environment information
- cordova-ios v6.2.0
- cordova-plugin-background-mode v0.7.3
Related information
- Solution taken from cordova-plugin-ionic-webview: https://github.com/ionic-team/cordova-plugin-ionic-webview/blob/cb1f0266f6c3c37efae6c9a66bfca3d49884fdb6/src/ios/CDVWKWebViewEngine.m#L170
I hope that help you and if you see something that i need improve, let me know.
On the other hand, since the plugin seems abandoned. What can we do as a community to keep it going?
Thank you! This worked well for me
anyone have a fix for capacitor?