diff --git a/src/ios/APPBackgroundMode.h b/src/ios/APPBackgroundMode.h index b039590..30e1480 100644 --- a/src/ios/APPBackgroundMode.h +++ b/src/ios/APPBackgroundMode.h @@ -11,7 +11,9 @@ #import #import -@interface APPBackgroundMode : CDVPlugin +@interface APPBackgroundMode : CDVPlugin { + BOOL _activated; +} // Aktiviert den Hintergrundmodus - (void) activate:(CDVInvokedUrlCommand *)command; diff --git a/src/ios/APPBackgroundMode.m b/src/ios/APPBackgroundMode.m index d1bfb20..fbac767 100644 --- a/src/ios/APPBackgroundMode.m +++ b/src/ios/APPBackgroundMode.m @@ -11,6 +11,8 @@ @interface APPBackgroundMode (PrivateMethods) +// Registriert die Listener für die (sleep/resume) Events +- (void) observeLifeCycle:(CDVInvokedUrlCommand *)command; // Aktiviert den Hintergrundmodus - (void) activateMode; // Deaktiviert den Hintergrundmodus @@ -22,6 +24,16 @@ @synthesize locationManager; +/** + * @js-interface + * + * Registriert die Listener für die (sleep/resume) Events. + */ +- (void) observeLifeCycle:(CDVInvokedUrlCommand *)command +{ + // Methode pluginInitialize wird aufgerufen, falls Instanz erstellt wurde +} + /** * @js-interface * @@ -49,6 +61,43 @@ */ - (void) activateMode { + _activated = true; +} + +/** + * @obj-c-interface + * + * Deaktiviert den Hintergrundmodus. + */ +- (void) deactivateMode +{ + _activated = false; +} + +/** + * Registriert die Listener für die (sleep/resume) Events und startet bzw. stoppt die Geo-Lokalisierung. + */ +- (void) pluginInitialize +{ + [self activateMode]; + + if (&UIApplicationDidEnterBackgroundNotification && &UIApplicationWillEnterForegroundNotification) { + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startUpdatingLocation) name:UIApplicationDidEnterBackgroundNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopUpdatingLocation) name:UIApplicationWillEnterForegroundNotification object:nil]; + } else { + [self startUpdatingLocation]; + } +} + +/** + * Startet das Aktualisieren des Standpunktes. + */ +- (void) startUpdatingLocation +{ + if (_activated == false) { + return; + }; + if (!locationManager) { locationManager = [[CLLocationManager alloc] init]; }; @@ -64,28 +113,14 @@ } /** - * @obj-c-interface - * - * Deaktiviert den Hintergrundmodus. + * Beendet das Aktualisieren des Standpunktes. */ -- (void) deactivateMode +- (void) stopUpdatingLocation { if (locationManager) { + // Beendet das Aktualisieren des Standpunktes [locationManager stopUpdatingLocation]; }; } -/** - * Registriert die Listener für die (sleep/resume) Events und startet bzw. stoppt die Geo-Lokalisierung. - */ -- (void) pluginInitialize -{ - if (&UIApplicationDidEnterBackgroundNotification && &UIApplicationWillEnterForegroundNotification) { - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(activateMode) name:UIApplicationDidEnterBackgroundNotification object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deactivateMode) name:UIApplicationWillEnterForegroundNotification object:nil]; - } else { - [self activateMode]; - } -} - @end \ No newline at end of file diff --git a/www/background-mode.js b/www/background-mode.js index 78f5241..0107061 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -8,11 +8,14 @@ */ var BackgroundMode = function () { - + // Registriert die Listener für die (sleep/resume) Events + cordova.exec(null, null, 'BackgroundMode', 'observeLifeCycle', []); }; BackgroundMode.prototype = { /** + * @public + * * Aktiviert den Hintergrundmodus. */ activate: function () { @@ -20,9 +23,11 @@ BackgroundMode.prototype = { }, /** + * @public + * * Deaktiviert den Hintergrundmodus */ - deactivate: function (badge) { + deactivate: function () { cordova.exec(null, null, 'BackgroundMode', 'deactivate', []); } };