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