From a5e60b235d8a50749bbdd51e9c35acc9c2b1d737 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Mon, 16 Jan 2017 15:15:15 +0100 Subject: [PATCH] Fix fireEvent on ios --- src/ios/APPBackgroundMode.h | 8 +++--- src/ios/APPBackgroundMode.m | 51 ++++++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/ios/APPBackgroundMode.h b/src/ios/APPBackgroundMode.h index 1f160dc..3c729b7 100644 --- a/src/ios/APPBackgroundMode.h +++ b/src/ios/APPBackgroundMode.h @@ -19,9 +19,7 @@ under the License. */ -#import #import -#import #import @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 \ No newline at end of file +@end diff --git a/src/ios/APPBackgroundMode.m b/src/ios/APPBackgroundMode.m index d629a19..7064560 100644 --- a/src/ios/APPBackgroundMode.m +++ b/src/ios/APPBackgroundMode.m @@ -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]; }