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

View File

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