[iOS] Benutzerort erfassen: Erläuterung

Ich habe eine App, die den Benutzerstandort erfasst und diese Informationen an meinen Server sendet.

Ich habe den LocationManager-Dienst gestartet mit:

- (CLLocationManager *)locationManager {
    if (_locationManager == nil) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        isUpdatingLocation = NO;
    }
    return _locationManager; 
}

-(void) startLookingForLocation{

    [self locationManager].desiredAccuracy = kCLLocationAccuracyBest;
    [self locationManager].distanceFilter = 250;

    if([[self locationManager]respondsToSelector:@selector(setPausesLocationUpdatesAutomatically:)])
        [[self locationManager] setPausesLocationUpdatesAutomatically:YES];

    if([[self locationManager]respondsToSelector:@selector(setActivityType:) ])
        [[self locationManager] setActivityType:CLActivityTypeFitness];

    [[self locationManager] startUpdatingLocation];
    [[self locationManager] startUpdatingHeading];

    isUpdatingLocation = YES;
}

und stoppen mit:

-(void) stopLookingForLocation{

    [[self locationManager] stopUpdatingLocation];
    [[self locationManager] stopUpdatingHeading];

    isUpdatingLocation = NO;

}

Wenn ich also eine Änderung des Benutzerorts feststelle:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    [self setCurrentLocation:newLocation];

    BOOL isInBackground = NO;
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
    {
        isInBackground = YES;
    }

    if (isInBackground)
    {
        UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication]
                  beginBackgroundTaskWithExpirationHandler:
                  ^{
                      [[UIApplication sharedApplication] endBackgroundTask:bgTask];
                  }];

        // Send user-location to my server

        if (bgTask != UIBackgroundTaskInvalid)
        {
            [[UIApplication sharedApplication] endBackgroundTask:bgTask];
            bgTask = UIBackgroundTaskInvalid;
        }
    }
    else
    {
        // Send user-location to my server
    }

}

und alles funktioniert einwandfrei. (Schätze ich).

Wenn die App in den Hintergrundmodus wechselt, rufe ich diese Methode in appDelegate - (void) applicationDidEnterBackground auf: (UIApplication *) application {[locationManager startMonitoringSignificantLocationChanges]; }

und wenn die App in den Vordergrund zurückkehrt:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [locationManager stopMonitoringSignificantLocationChanges];
}

Aber ich habe einige Zweifel. Auf diese Weise kommuniziert meine App den Standort weiterhin mit dem Server, wenn die App geschlossen ist.

Wie kann ich einen Standort nur senden, wenn ich mich im Hintergrund oder im Vordergrund befinde?

EDIT: Ich habe auch eingestellt:

UIBackgroundModes-Speicherort

Antworten auf die Frage(1)

Ihre Antwort auf die Frage