mirror of
https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background
synced 2024-12-23 13:57:35 +00:00
With webkit the plugin is enabled by default without the need to play audio
This commit is contained in:
parent
4764ffe14c
commit
e25beda8c9
@ -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);
|
||||
|
@ -103,4 +103,3 @@ IMP class_swizzleSelector(Class clazz, SEL selector, IMP newImpl)
|
||||
|
||||
return class_replaceMethod(clazz, selector, newImpl, types);
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user