2013-10-08 09:00:36 +00:00
|
|
|
/**
|
|
|
|
* APPBackgroundMode.m
|
|
|
|
* Cordova BackgroundMode Plugin
|
|
|
|
*
|
|
|
|
* Created by Sebastian Katzer (github.com/katzer) on 08/10/2013.
|
|
|
|
* Copyright 2013 Sebastian Katzer. All rights reserved.
|
|
|
|
* GPL v2 licensed
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "APPBackgroundMode.h"
|
|
|
|
|
2013-10-09 08:50:15 +00:00
|
|
|
@interface APPBackgroundMode (PrivateMethods)
|
|
|
|
|
2013-10-09 09:40:06 +00:00
|
|
|
// Registriert die Listener für die (sleep/resume) Events
|
|
|
|
- (void) observeLifeCycle:(CDVInvokedUrlCommand *)command;
|
2013-10-09 08:50:15 +00:00
|
|
|
// Aktiviert den Hintergrundmodus
|
2013-10-09 12:54:23 +00:00
|
|
|
- (void) enableMode;
|
2013-10-09 08:50:15 +00:00
|
|
|
// Deaktiviert den Hintergrundmodus
|
2013-10-09 12:54:23 +00:00
|
|
|
- (void) disableMode;
|
2013-10-09 08:50:15 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2013-10-08 09:00:36 +00:00
|
|
|
@implementation APPBackgroundMode
|
|
|
|
|
2013-10-08 12:36:19 +00:00
|
|
|
@synthesize locationManager;
|
|
|
|
|
2013-10-09 09:40:06 +00:00
|
|
|
/**
|
|
|
|
* @js-interface
|
|
|
|
*
|
|
|
|
* Registriert die Listener für die (sleep/resume) Events.
|
|
|
|
*/
|
|
|
|
- (void) observeLifeCycle:(CDVInvokedUrlCommand *)command
|
|
|
|
{
|
|
|
|
// Methode pluginInitialize wird aufgerufen, falls Instanz erstellt wurde
|
|
|
|
}
|
|
|
|
|
2013-10-09 08:50:15 +00:00
|
|
|
/**
|
|
|
|
* @js-interface
|
|
|
|
*
|
|
|
|
* Aktiviert den Hintergrundmodus.
|
|
|
|
*/
|
2013-10-09 12:54:23 +00:00
|
|
|
- (void) enable:(CDVInvokedUrlCommand *)command
|
2013-10-09 08:50:15 +00:00
|
|
|
{
|
2013-10-09 12:54:23 +00:00
|
|
|
[self enableMode];
|
2013-10-09 08:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @js-interface
|
|
|
|
*
|
|
|
|
* Deaktiviert den Hintergrundmodus.
|
|
|
|
*/
|
2013-10-09 12:54:23 +00:00
|
|
|
- (void) disable:(CDVInvokedUrlCommand *)command
|
2013-10-09 08:50:15 +00:00
|
|
|
{
|
2013-10-09 12:54:23 +00:00
|
|
|
[self disableMode];
|
2013-10-09 08:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Aktiviert den Hintergrundmodus.
|
|
|
|
*/
|
2013-10-09 12:54:23 +00:00
|
|
|
- (void) enableMode
|
2013-10-08 09:00:36 +00:00
|
|
|
{
|
2013-10-09 12:54:23 +00:00
|
|
|
_enabled = true;
|
2013-10-09 09:40:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deaktiviert den Hintergrundmodus.
|
|
|
|
*/
|
2013-10-09 12:54:23 +00:00
|
|
|
- (void) disableMode
|
2013-10-09 09:40:06 +00:00
|
|
|
{
|
2013-10-09 12:54:23 +00:00
|
|
|
_enabled = false;
|
2013-10-09 09:40:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registriert die Listener für die (sleep/resume) Events und startet bzw. stoppt die Geo-Lokalisierung.
|
|
|
|
*/
|
|
|
|
- (void) pluginInitialize
|
|
|
|
{
|
2013-10-09 12:54:23 +00:00
|
|
|
[self enableMode];
|
2013-10-09 09:40:06 +00:00
|
|
|
|
|
|
|
if (&UIApplicationDidEnterBackgroundNotification && &UIApplicationWillEnterForegroundNotification) {
|
2013-10-09 12:54:23 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(activateMode) name:UIApplicationDidEnterBackgroundNotification object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deactivateMode) name:UIApplicationWillEnterForegroundNotification object:nil];
|
2013-10-09 09:40:06 +00:00
|
|
|
} else {
|
2013-10-09 12:54:23 +00:00
|
|
|
[self activateMode];
|
2013-10-09 09:40:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Startet das Aktualisieren des Standpunktes.
|
|
|
|
*/
|
2013-10-09 12:54:23 +00:00
|
|
|
- (void) activateMode
|
2013-10-09 09:40:06 +00:00
|
|
|
{
|
2013-10-09 12:54:23 +00:00
|
|
|
if (_enabled == false) {
|
2013-10-09 09:40:06 +00:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2013-10-08 12:36:19 +00:00
|
|
|
if (!locationManager) {
|
|
|
|
locationManager = [[CLLocationManager alloc] init];
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef __IPHONE_6_0
|
|
|
|
locationManager.activityType = CLActivityTypeFitness;
|
|
|
|
#endif
|
2013-10-08 09:00:36 +00:00
|
|
|
|
2013-10-08 12:36:19 +00:00
|
|
|
// Empfängt nur Nachrichten, wenn sich die Position um 1km geändert hat
|
|
|
|
locationManager.distanceFilter = 1000;
|
|
|
|
// Startet das Aktualisieren des Standpunktes
|
|
|
|
[locationManager startUpdatingLocation];
|
2013-10-08 09:00:36 +00:00
|
|
|
}
|
|
|
|
|
2013-10-09 08:50:15 +00:00
|
|
|
/**
|
2013-10-09 09:40:06 +00:00
|
|
|
* Beendet das Aktualisieren des Standpunktes.
|
2013-10-09 08:50:15 +00:00
|
|
|
*/
|
2013-10-09 12:54:23 +00:00
|
|
|
- (void) deactivateMode
|
2013-10-08 09:00:36 +00:00
|
|
|
{
|
2013-10-08 12:36:19 +00:00
|
|
|
if (locationManager) {
|
2013-10-09 09:40:06 +00:00
|
|
|
// Beendet das Aktualisieren des Standpunktes
|
2013-10-08 12:36:19 +00:00
|
|
|
[locationManager stopUpdatingLocation];
|
|
|
|
};
|
|
|
|
}
|
2013-10-08 09:00:36 +00:00
|
|
|
|
|
|
|
@end
|