mirror of
https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background
synced 2024-11-22 07:14:54 +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
|
- (void) pluginInitialize
|
||||||
{
|
{
|
||||||
[self.commandDelegate runInBackground:^{
|
[self.commandDelegate runInBackground:^{
|
||||||
enabled = NO;
|
enabled = [self.class isRunningWebKit];
|
||||||
[self configureAudioPlayer];
|
[self configureAudioPlayer];
|
||||||
[self configureAudioSession];
|
[self configureAudioSession];
|
||||||
[self observeLifeCycle];
|
[self observeLifeCycle];
|
||||||
@ -77,6 +77,9 @@ NSString* const kAPPBackgroundEventFailure = @"failure";
|
|||||||
name:UIApplicationWillEnterForegroundNotification
|
name:UIApplicationWillEnterForegroundNotification
|
||||||
object:nil];
|
object:nil];
|
||||||
|
|
||||||
|
if ([self.class isRunningWebKit])
|
||||||
|
return;
|
||||||
|
|
||||||
[listener addObserver:self
|
[listener addObserver:self
|
||||||
selector:@selector(handleAudioSessionInterruption:)
|
selector:@selector(handleAudioSessionInterruption:)
|
||||||
name:AVAudioSessionInterruptionNotification
|
name:AVAudioSessionInterruptionNotification
|
||||||
@ -92,6 +95,9 @@ NSString* const kAPPBackgroundEventFailure = @"failure";
|
|||||||
*/
|
*/
|
||||||
- (void) enable:(CDVInvokedUrlCommand*)command
|
- (void) enable:(CDVInvokedUrlCommand*)command
|
||||||
{
|
{
|
||||||
|
if (enabled)
|
||||||
|
return;
|
||||||
|
|
||||||
enabled = YES;
|
enabled = YES;
|
||||||
[self execCallback:command];
|
[self execCallback:command];
|
||||||
}
|
}
|
||||||
@ -102,6 +108,9 @@ NSString* const kAPPBackgroundEventFailure = @"failure";
|
|||||||
*/
|
*/
|
||||||
- (void) disable:(CDVInvokedUrlCommand*)command
|
- (void) disable:(CDVInvokedUrlCommand*)command
|
||||||
{
|
{
|
||||||
|
if (!enabled || [self.class isRunningWebKit])
|
||||||
|
return;
|
||||||
|
|
||||||
enabled = NO;
|
enabled = NO;
|
||||||
[self stopKeepingAwake];
|
[self stopKeepingAwake];
|
||||||
[self execCallback:command];
|
[self execCallback:command];
|
||||||
@ -118,7 +127,10 @@ NSString* const kAPPBackgroundEventFailure = @"failure";
|
|||||||
if (!enabled)
|
if (!enabled)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
if (![self.class isRunningWebKit]) {
|
||||||
[audioPlayer play];
|
[audioPlayer play];
|
||||||
|
}
|
||||||
|
|
||||||
[self fireEvent:kAPPBackgroundEventActivate];
|
[self fireEvent:kAPPBackgroundEventActivate];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,7 +143,7 @@ NSString* const kAPPBackgroundEventFailure = @"failure";
|
|||||||
NSLog(@"BackgroundMode: On simulator apps never pause in background!");
|
NSLog(@"BackgroundMode: On simulator apps never pause in background!");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (audioPlayer.isPlaying) {
|
if (audioPlayer.isPlaying || [self.class isRunningWebKit]) {
|
||||||
[self fireEvent:kAPPBackgroundEventDeactivate];
|
[self fireEvent:kAPPBackgroundEventDeactivate];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -164,6 +176,9 @@ NSString* const kAPPBackgroundEventFailure = @"failure";
|
|||||||
AVAudioSession* session = [AVAudioSession
|
AVAudioSession* session = [AVAudioSession
|
||||||
sharedInstance];
|
sharedInstance];
|
||||||
|
|
||||||
|
if ([self.class isRunningWebKit])
|
||||||
|
return;
|
||||||
|
|
||||||
// Don't activate the audio session yet
|
// Don't activate the audio session yet
|
||||||
[session setActive:NO error:NULL];
|
[session setActive:NO error:NULL];
|
||||||
|
|
||||||
@ -201,6 +216,14 @@ NSString* const kAPPBackgroundEventFailure = @"failure";
|
|||||||
[self keepAwake];
|
[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.
|
* Method to fire an event with some parameters in the browser.
|
||||||
*/
|
*/
|
||||||
@ -231,15 +254,12 @@ NSString* const kAPPBackgroundEventFailure = @"failure";
|
|||||||
*/
|
*/
|
||||||
+ (void) swizzleWKWebViewEngine
|
+ (void) swizzleWKWebViewEngine
|
||||||
{
|
{
|
||||||
if (!IsAtLeastiOSVersion(@"8.0"))
|
if (![self isRunningWebKit])
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Class wkWebViewEngineCls = NSClassFromString(@"CDVWKWebViewEngine");
|
Class wkWebViewEngineCls = NSClassFromString(@"CDVWKWebViewEngine");
|
||||||
SEL selector = NSSelectorFromString(@"createConfigurationFromSettings:");
|
SEL selector = NSSelectorFromString(@"createConfigurationFromSettings:");
|
||||||
|
|
||||||
if (!wkWebViewEngineCls)
|
|
||||||
return;
|
|
||||||
|
|
||||||
SwizzleSelectorWithBlock_Begin(wkWebViewEngineCls, selector)
|
SwizzleSelectorWithBlock_Begin(wkWebViewEngineCls, selector)
|
||||||
^(CDVPlugin *self, NSDictionary *settings) {
|
^(CDVPlugin *self, NSDictionary *settings) {
|
||||||
id obj = ((id (*)(id, SEL, NSDictionary*))_imp)(self, _cmd, 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);
|
return class_replaceMethod(clazz, selector, newImpl, types);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,11 +36,12 @@ exports.enable = function () {
|
|||||||
if (this.isEnabled())
|
if (this.isEnabled())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var me = this,
|
var fn = function () {
|
||||||
fireEvent = function () { me.fireEvent('enable'); };
|
exports._isEnabled = true;
|
||||||
|
exports.fireEvent('enable');
|
||||||
|
};
|
||||||
|
|
||||||
this._isEnabled = true;
|
cordova.exec(fn, null, 'BackgroundMode', 'enable', []);
|
||||||
cordova.exec(fireEvent, null, 'BackgroundMode', 'enable', []);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -51,11 +52,12 @@ exports.disable = function () {
|
|||||||
if (!this.isEnabled())
|
if (!this.isEnabled())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var me = this,
|
var fn = function () {
|
||||||
fireEvent = function () { me.fireEvent('disable'); };
|
exports._isEnabled = false;
|
||||||
|
exports.fireEvent('disable');
|
||||||
|
};
|
||||||
|
|
||||||
this._isEnabled = false;
|
cordova.exec(fn, null, 'BackgroundMode', 'disable', []);
|
||||||
cordova.exec(fireEvent, null, 'BackgroundMode', 'disable', []);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -302,7 +304,7 @@ exports.mergeWithDefaults = function (options) {
|
|||||||
*
|
*
|
||||||
* Flag indicates if the mode is enabled.
|
* Flag indicates if the mode is enabled.
|
||||||
*/
|
*/
|
||||||
exports._isEnabled = false;
|
exports._isEnabled = window.webkit !== undefined;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
@ -318,7 +320,7 @@ exports._isActive = false;
|
|||||||
*/
|
*/
|
||||||
exports._defaults = {
|
exports._defaults = {
|
||||||
title: 'App is running in background',
|
title: 'App is running in background',
|
||||||
text: "Doing heavy tasks.",
|
text: 'Doing heavy tasks.',
|
||||||
ticker: 'Running in background',
|
ticker: 'Running in background',
|
||||||
bigText: false,
|
bigText: false,
|
||||||
resume: true,
|
resume: true,
|
||||||
|
Loading…
Reference in New Issue
Block a user