[iOS]capturing user location: clarification

2019-04-01 01:22发布

问题:

I've an app that capture user location and send this info to my server.

I've started the LocationManager service with:

- (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;
}

and stopping it with:

-(void) stopLookingForLocation{

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

    isUpdatingLocation = NO;

}

So, when I detect a change of user location:

- (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
    }

}

and all works correctly. (I suppose).

When app go into background mode, I invoke this method within appDelegate - (void)applicationDidEnterBackground:(UIApplication *)application { [locationManager startMonitoringSignificantLocationChanges]; }

and when app return to foreground:

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

But I have some doubts. In this way, if app is in closed status, my app continues to comunicate location to server?

How I can send location only when I'm in background or foreground status?

EDIT: I've also set:

UIBackgroundModes location

回答1:

There is no need of this code this will active your application in background for Little time you have to mention Required background modes in info.plist of the project and the value should be App registers for location updates.

only Set UIBackgroundModes location in info.plist and it works in background this may help you.

//In app deligate 

#pragma mark -
#pragma mark  GPS Service

//========== start GPS SERVICE ===========
-(void)startGpsService
{

    locationManager=[[CLLocationManager alloc] init];
    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    locationManager.distanceFilter=10.0;
    [locationManager startUpdatingLocation];
    [self performSelectorInBackground:@selector(sendDataToServer) withObject:nil];
}

#pragma mark -
#pragma mark CLLocation Manager Delegates

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation
{
      [self performSelectorInBackground:@selector(sendDataToServer) withObject:nil];

}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // No need to do any thing 
   /* UIApplication *app = [UIApplication sharedApplication];
      UIBackgroundTaskIdentifier bgTask = 0;
      bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
      [app endBackgroundTask:bgTask];
       }];
*/
}