Sending Latitude and Longitude to Server when app is in background

前端 未结 3 1164
难免孤独
难免孤独 2020-12-05 16:59

I have gone through so many links, even after that I haven\'t found a proper solution for getting latitude and longitude.

Periodic iOS background location updates

3条回答
  •  情话喂你
    2020-12-05 17:38

    Building on Santos answer which has most of the important steps to make background positioning work, I have clarified and corrected some details.

    Project settings

    You should add these keys to your Xcode Target Info property list. Make sure to add a valid description to each of them, or your app might fail approval.

    • NSLocationAlwaysUsageDescription
    • NSLocationWhenInUseUsageDescription
    • NSLocationAlwaysAndWhenInUseUsageDescription

    Next, in Target Capabilities, Turn on Background Modes and check Location updates and Background fetch. Location updates will enable locations in the background and background fetch will allow you to use the network in the background.

    Start monitoring

    Start by creating an instance of CLLocationManager, configure it and turn on background updates, then start it. Although the code below uses the most common function startUpdatingLocation to get locations, there are several other services to use. It is important to choose the most suitable service as this impacts greatly on battery usage and if the app will be re-launched or not by iOS. See below for more info on this.

    // Declare as properties in your class
    @property (strong, nonatomic) CLLocationManager *locationManager;
    @property (strong, nonatomic) CLLocation *lastLocation;
    
    // Call to start
    - (void)initializeAndStartLocationService {
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
    
        // Must be set for background operation
        self.locationManager.allowsBackgroundLocationUpdates = YES;
    
        // Optional configuration
        self.locationManager.distanceFilter = kCLDistanceFilterNone;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationManager.pausesLocationUpdatesAutomatically = YES;
        self.locationManager.showsBackgroundLocationIndicator = YES;
    
        // Authorize - the lazy way
        [self.locationManager requestAlwaysAuthorization];
    
        // Start the standard location service, there are others
        [self.locationManager startUpdatingLocation];
    }
    
    // Delegate method that will receive location updates
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
        // Keep the last received location
        self.lastLocation = [locations lastObject];
    
        NSLog(@"New position %f, %f", self.lastLocation.coordinate.latitude, self.lastLocation.coordinate.longitude);
    
        // Do something with the location or retrieve the location later
        //       :
    }
    

    Stop monitoring

    Don't forget to stop monitoring to conserve battery. You can start and stop several times on a single instance of CLLocationManager.

    - (void)dealloc {
        [self.locationManager stopUpdatingLocation];
    }
    

    Automatic app re-launch considerations

    When your app is running in the background it can (and actually frequently will after some time) be terminated by iOS. Depending on the type of location updates you are using, iOS will or will not re-launch your app automatically for you. In cases where iOS do not re-launch, the user must start your app again to continue background processing.

    Read more on this page.

    Read more

    CLLocationManager - Core Location | Apple Documentation
    Handling Location Events in the Background | Apple Documentation

提交回复
热议问题