mirror of
https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background
synced 2024-11-15 03:54:54 +00:00
Fix fireEvent on ios
This commit is contained in:
parent
8b434e041c
commit
a5e60b235d
@ -19,9 +19,7 @@
|
|||||||
under the License.
|
under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#import <Foundation/Foundation.h>
|
|
||||||
#import <AVFoundation/AVFoundation.h>
|
#import <AVFoundation/AVFoundation.h>
|
||||||
#import <AudioToolbox/AudioToolbox.h>
|
|
||||||
#import <Cordova/CDVPlugin.h>
|
#import <Cordova/CDVPlugin.h>
|
||||||
|
|
||||||
@interface APPBackgroundMode : CDVPlugin {
|
@interface APPBackgroundMode : CDVPlugin {
|
||||||
|
@ -29,7 +29,7 @@ NSString *const kAPPBackgroundEventDeactivate = @"deactivate";
|
|||||||
NSString *const kAPPBackgroundEventFailure = @"failure";
|
NSString *const kAPPBackgroundEventFailure = @"failure";
|
||||||
|
|
||||||
#pragma mark -
|
#pragma mark -
|
||||||
#pragma mark Initialization methods
|
#pragma mark Initialization
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize the plugin.
|
* Initialize the plugin.
|
||||||
@ -73,7 +73,7 @@ NSString *const kAPPBackgroundEventFailure = @"failure";
|
|||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark -
|
#pragma mark -
|
||||||
#pragma mark Interface methods
|
#pragma mark Interface
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enable the mode to stay awake
|
* Enable the mode to stay awake
|
||||||
@ -96,12 +96,13 @@ NSString *const kAPPBackgroundEventFailure = @"failure";
|
|||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark -
|
#pragma mark -
|
||||||
#pragma mark Core methods
|
#pragma mark Core
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Keep the app awake.
|
* Keep the app awake.
|
||||||
*/
|
*/
|
||||||
- (void) keepAwake {
|
- (void) keepAwake
|
||||||
|
{
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
[audioPlayer play];
|
[audioPlayer play];
|
||||||
[self fireEvent:kAPPBackgroundEventActivate withParams:NULL];
|
[self fireEvent:kAPPBackgroundEventActivate withParams:NULL];
|
||||||
@ -111,7 +112,8 @@ NSString *const kAPPBackgroundEventFailure = @"failure";
|
|||||||
/**
|
/**
|
||||||
* Let the app going to sleep.
|
* Let the app going to sleep.
|
||||||
*/
|
*/
|
||||||
- (void) stopKeepingAwake {
|
- (void) stopKeepingAwake
|
||||||
|
{
|
||||||
|
|
||||||
if (TARGET_IPHONE_SIMULATOR) {
|
if (TARGET_IPHONE_SIMULATOR) {
|
||||||
NSLog(@"BackgroundMode: On simulator apps never pause in background!");
|
NSLog(@"BackgroundMode: On simulator apps never pause in background!");
|
||||||
@ -127,46 +129,50 @@ NSString *const kAPPBackgroundEventFailure = @"failure";
|
|||||||
/**
|
/**
|
||||||
* Configure the audio player.
|
* Configure the audio player.
|
||||||
*/
|
*/
|
||||||
- (void) configureAudioPlayer {
|
- (void) configureAudioPlayer
|
||||||
NSString* path = [[NSBundle mainBundle] pathForResource:@"appbeep"
|
{
|
||||||
ofType:@"wav"];
|
NSString* path = [[NSBundle mainBundle]
|
||||||
|
pathForResource:@"appbeep" ofType:@"wav"];
|
||||||
|
|
||||||
NSURL* url = [NSURL fileURLWithPath:path];
|
NSURL* url = [NSURL fileURLWithPath:path];
|
||||||
|
|
||||||
|
|
||||||
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url
|
audioPlayer = [[AVAudioPlayer alloc]
|
||||||
error:NULL];
|
initWithContentsOfURL:url error:NULL];
|
||||||
|
|
||||||
// Silent
|
|
||||||
audioPlayer.volume = 0;
|
audioPlayer.volume = 0;
|
||||||
// Infinite
|
|
||||||
audioPlayer.numberOfLoops = -1;
|
audioPlayer.numberOfLoops = -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configure the audio session.
|
* Configure the audio session.
|
||||||
*/
|
*/
|
||||||
- (void) configureAudioSession {
|
- (void) configureAudioSession
|
||||||
|
{
|
||||||
AVAudioSession* session = [AVAudioSession
|
AVAudioSession* session = [AVAudioSession
|
||||||
sharedInstance];
|
sharedInstance];
|
||||||
|
|
||||||
// Don't activate the audio session yet
|
// Don't activate the audio session yet
|
||||||
[session setActive:NO error:NULL];
|
[session setActive:NO error:NULL];
|
||||||
|
|
||||||
// Play music even in background and dont stop playing music
|
// Play music even in background and dont stop playing music
|
||||||
// even another app starts playing sound
|
// even another app starts playing sound
|
||||||
[session setCategory:AVAudioSessionCategoryPlayback
|
[session setCategory:AVAudioSessionCategoryPlayback
|
||||||
withOptions:AVAudioSessionCategoryOptionMixWithOthers
|
withOptions:AVAudioSessionCategoryOptionMixWithOthers
|
||||||
error:NULL];
|
error:NULL];
|
||||||
|
|
||||||
// Active the audio session
|
// Active the audio session
|
||||||
[session setActive:YES error:NULL];
|
[session setActive:YES error:NULL];
|
||||||
};
|
};
|
||||||
|
|
||||||
#pragma mark -
|
#pragma mark -
|
||||||
#pragma mark Helper methods
|
#pragma mark Helper
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Restart playing sound when interrupted by phone calls.
|
* Restart playing sound when interrupted by phone calls.
|
||||||
*/
|
*/
|
||||||
- (void) handleAudioSessionInterruption:(NSNotification*)notification {
|
- (void) handleAudioSessionInterruption:(NSNotification*)notification
|
||||||
|
{
|
||||||
[self fireEvent:kAPPBackgroundEventDeactivate withParams:NULL];
|
[self fireEvent:kAPPBackgroundEventDeactivate withParams:NULL];
|
||||||
[self keepAwake];
|
[self keepAwake];
|
||||||
}
|
}
|
||||||
@ -181,10 +187,13 @@ NSString *const kAPPBackgroundEventFailure = @"failure";
|
|||||||
NSString* flag = [NSString stringWithFormat:@"%@._isActive=%@;",
|
NSString* flag = [NSString stringWithFormat:@"%@._isActive=%@;",
|
||||||
kAPPBackgroundJsNamespace, active];
|
kAPPBackgroundJsNamespace, active];
|
||||||
|
|
||||||
NSString* fn = [NSString stringWithFormat:@"setTimeout('%@.on%@(%@)',0);",
|
NSString* depFn = [NSString stringWithFormat:@"%@.on%@(%@);",
|
||||||
kAPPBackgroundJsNamespace, event, params];
|
kAPPBackgroundJsNamespace, event, params];
|
||||||
|
|
||||||
NSString* js = [flag stringByAppendingString:fn];
|
NSString* fn = [NSString stringWithFormat:@"%@.fireEvent('%@',%@);",
|
||||||
|
kAPPBackgroundJsNamespace, event, params];
|
||||||
|
|
||||||
|
NSString* js = [NSString stringWithFormat:@"%@%@%@", flag, depFn, fn];
|
||||||
|
|
||||||
[self.commandDelegate evalJs:js];
|
[self.commandDelegate evalJs:js];
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user