Fix fireEvent on ios

This commit is contained in:
Sebastián Katzer 2017-01-16 15:15:15 +01:00
parent 8b434e041c
commit a5e60b235d
2 changed files with 33 additions and 26 deletions

View File

@ -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 {
@ -30,8 +28,8 @@
}
// Activate the background mode
- (void) enable:(CDVInvokedUrlCommand *)command;
- (void) enable:(CDVInvokedUrlCommand*)command;
// Deactivate the background mode
- (void) disable:(CDVInvokedUrlCommand *)command;
- (void) disable:(CDVInvokedUrlCommand*)command;
@end

View File

@ -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,13 +73,13 @@ NSString *const kAPPBackgroundEventFailure = @"failure";
}
#pragma mark -
#pragma mark Interface methods
#pragma mark Interface
/**
* Enable the mode to stay awake
* when switching to background for the next time.
*/
- (void) enable:(CDVInvokedUrlCommand *)command
- (void) enable:(CDVInvokedUrlCommand*)command
{
enabled = YES;
}
@ -88,7 +88,7 @@ NSString *const kAPPBackgroundEventFailure = @"failure";
* Disable the background mode
* and stop being active in background.
*/
- (void) disable:(CDVInvokedUrlCommand *)command
- (void) disable:(CDVInvokedUrlCommand*)command
{
enabled = NO;
@ -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.volume = 0;
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];
}
@ -179,12 +185,15 @@ NSString *const kAPPBackgroundEventFailure = @"failure";
NSString* active = [event isEqualToString:kAPPBackgroundEventActivate] ? @"true" : @"false";
NSString* flag = [NSString stringWithFormat:@"%@._isActive=%@;",
kAPPBackgroundJsNamespace, active];
kAPPBackgroundJsNamespace, active];
NSString* fn = [NSString stringWithFormat:@"setTimeout('%@.on%@(%@)',0);",
NSString* depFn = [NSString stringWithFormat:@"%@.on%@(%@);",
kAPPBackgroundJsNamespace, event, params];
NSString* fn = [NSString stringWithFormat:@"%@.fireEvent('%@',%@);",
kAPPBackgroundJsNamespace, event, params];
NSString* js = [flag stringByAppendingString:fn];
NSString* js = [NSString stringWithFormat:@"%@%@%@", flag, depFn, fn];
[self.commandDelegate evalJs:js];
}