cordova-plugin-background-mode icon indicating copy to clipboard operation
cordova-plugin-background-mode copied to clipboard

iOS 12.x not working - workaround

Open fgarcia5 opened this issue 3 years ago • 2 comments

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:

  1. In config.xml add <preference name="WKSuspendInBackground" value="false" />

  2. 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?

fgarcia5 avatar May 18 '21 13:05 fgarcia5

Thank you! This worked well for me

travisapple avatar Jun 14 '21 22:06 travisapple

anyone have a fix for capacitor?

alindzon avatar Oct 23 '22 23:10 alindzon