From e25beda8c966354dbeb50278823a7687c9d8e125 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Wed, 18 Jan 2017 18:00:57 +0100 Subject: [PATCH] With webkit the plugin is enabled by default without the need to play audio --- src/ios/APPBackgroundMode.m | 36 ++++++++++++++++++++++++++++-------- src/ios/APPMethodMagic.m | 1 - www/background-mode.js | 34 ++++++++++++++++++---------------- 3 files changed, 46 insertions(+), 25 deletions(-) diff --git a/src/ios/APPBackgroundMode.m b/src/ios/APPBackgroundMode.m index 6983cf2..a25f76c 100644 --- a/src/ios/APPBackgroundMode.m +++ b/src/ios/APPBackgroundMode.m @@ -52,7 +52,7 @@ NSString* const kAPPBackgroundEventFailure = @"failure"; - (void) pluginInitialize { [self.commandDelegate runInBackground:^{ - enabled = NO; + enabled = [self.class isRunningWebKit]; [self configureAudioPlayer]; [self configureAudioSession]; [self observeLifeCycle]; @@ -66,7 +66,7 @@ NSString* const kAPPBackgroundEventFailure = @"failure"; { NSNotificationCenter* listener = [NSNotificationCenter defaultCenter]; - + [listener addObserver:self selector:@selector(keepAwake) name:UIApplicationDidEnterBackgroundNotification @@ -76,6 +76,9 @@ NSString* const kAPPBackgroundEventFailure = @"failure"; selector:@selector(stopKeepingAwake) name:UIApplicationWillEnterForegroundNotification object:nil]; + + if ([self.class isRunningWebKit]) + return; [listener addObserver:self selector:@selector(handleAudioSessionInterruption:) @@ -92,6 +95,9 @@ NSString* const kAPPBackgroundEventFailure = @"failure"; */ - (void) enable:(CDVInvokedUrlCommand*)command { + if (enabled) + return; + enabled = YES; [self execCallback:command]; } @@ -102,6 +108,9 @@ NSString* const kAPPBackgroundEventFailure = @"failure"; */ - (void) disable:(CDVInvokedUrlCommand*)command { + if (!enabled || [self.class isRunningWebKit]) + return; + enabled = NO; [self stopKeepingAwake]; [self execCallback:command]; @@ -118,7 +127,10 @@ NSString* const kAPPBackgroundEventFailure = @"failure"; if (!enabled) return; - [audioPlayer play]; + if (![self.class isRunningWebKit]) { + [audioPlayer play]; + } + [self fireEvent:kAPPBackgroundEventActivate]; } @@ -131,7 +143,7 @@ NSString* const kAPPBackgroundEventFailure = @"failure"; NSLog(@"BackgroundMode: On simulator apps never pause in background!"); } - if (audioPlayer.isPlaying) { + if (audioPlayer.isPlaying || [self.class isRunningWebKit]) { [self fireEvent:kAPPBackgroundEventDeactivate]; } @@ -164,6 +176,9 @@ NSString* const kAPPBackgroundEventFailure = @"failure"; AVAudioSession* session = [AVAudioSession sharedInstance]; + if ([self.class isRunningWebKit]) + return; + // Don't activate the audio session yet [session setActive:NO error:NULL]; @@ -201,6 +216,14 @@ NSString* const kAPPBackgroundEventFailure = @"failure"; [self keepAwake]; } +/** + * Find out if the app runs inside the webkit powered webview. + */ ++ (BOOL) isRunningWebKit +{ + return IsAtLeastiOSVersion(@"8.0") && NSClassFromString(@"CDVWKWebViewEngine"); +} + /** * Method to fire an event with some parameters in the browser. */ @@ -231,15 +254,12 @@ NSString* const kAPPBackgroundEventFailure = @"failure"; */ + (void) swizzleWKWebViewEngine { - if (!IsAtLeastiOSVersion(@"8.0")) + if (![self isRunningWebKit]) return; Class wkWebViewEngineCls = NSClassFromString(@"CDVWKWebViewEngine"); SEL selector = NSSelectorFromString(@"createConfigurationFromSettings:"); - if (!wkWebViewEngineCls) - return; - SwizzleSelectorWithBlock_Begin(wkWebViewEngineCls, selector) ^(CDVPlugin *self, NSDictionary *settings) { id obj = ((id (*)(id, SEL, NSDictionary*))_imp)(self, _cmd, settings); diff --git a/src/ios/APPMethodMagic.m b/src/ios/APPMethodMagic.m index 3f3ee98..cb9cc35 100644 --- a/src/ios/APPMethodMagic.m +++ b/src/ios/APPMethodMagic.m @@ -103,4 +103,3 @@ IMP class_swizzleSelector(Class clazz, SEL selector, IMP newImpl) return class_replaceMethod(clazz, selector, newImpl, types); } - diff --git a/www/background-mode.js b/www/background-mode.js index 53199c6..a51afbf 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -36,11 +36,12 @@ exports.enable = function () { if (this.isEnabled()) return; - var me = this, - fireEvent = function () { me.fireEvent('enable'); }; + var fn = function () { + exports._isEnabled = true; + exports.fireEvent('enable'); + }; - this._isEnabled = true; - cordova.exec(fireEvent, null, 'BackgroundMode', 'enable', []); + cordova.exec(fn, null, 'BackgroundMode', 'enable', []); }; /** @@ -51,11 +52,12 @@ exports.disable = function () { if (!this.isEnabled()) return; - var me = this, - fireEvent = function () { me.fireEvent('disable'); }; + var fn = function () { + exports._isEnabled = false; + exports.fireEvent('disable'); + }; - this._isEnabled = false; - cordova.exec(fireEvent, null, 'BackgroundMode', 'disable', []); + cordova.exec(fn, null, 'BackgroundMode', 'disable', []); }; /** @@ -302,7 +304,7 @@ exports.mergeWithDefaults = function (options) { * * Flag indicates if the mode is enabled. */ -exports._isEnabled = false; +exports._isEnabled = window.webkit !== undefined; /** * @private @@ -317,14 +319,14 @@ exports._isActive = false; * Default values of all available options. */ exports._defaults = { - title: 'App is running in background', - text: "Doing heavy tasks.", - ticker: 'Running in background', + title: 'App is running in background', + text: 'Doing heavy tasks.', + ticker: 'Running in background', bigText: false, - resume: true, - silent: false, - color: undefined, - icon: 'icon' + resume: true, + silent: false, + color: undefined, + icon: 'icon' }; // Called before 'deviceready' listener will be called