Periodic iOS background location updates

前端 未结 9 1341
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 14:56

I\'m writing an application that requires background location updates with high accuracy and low frequency. The solution seems to be a background NSTimer t

9条回答
  •  伪装坚强ぢ
    2020-11-22 15:32

    I did write an app using Location services, app must send location every 10s. And it worked very well.

    Just use the "allowDeferredLocationUpdatesUntilTraveled:timeout" method, following Apple's doc.

    Steps are as follows:

    Required: Register background mode for update Location.

    1. Create LocationManger and startUpdatingLocation, with accuracy and filteredDistance as whatever you want:

    -(void) initLocationManager    
    {
        // Create the manager object
        self.locationManager = [[[CLLocationManager alloc] init] autorelease];
        _locationManager.delegate = self;
        // This is the most important property to set for the manager. It ultimately determines how the manager will
        // attempt to acquire location and thus, the amount of power that will be consumed.
        _locationManager.desiredAccuracy = 45;
        _locationManager.distanceFilter = 100;
        // Once configured, the location manager must be "started".
        [_locationManager startUpdatingLocation];
    }
    

    2. To keep app run forever using "allowDeferredLocationUpdatesUntilTraveled:timeout" method in background, you must restart updatingLocation with new parameter when app moves to background, like this:

    - (void)applicationWillResignActive:(UIApplication *)application {
         _isBackgroundMode = YES;
    
        [_locationManager stopUpdatingLocation];
        [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        [_locationManager setDistanceFilter:kCLDistanceFilterNone];
        _locationManager.pausesLocationUpdatesAutomatically = NO;
        _locationManager.activityType = CLActivityTypeAutomotiveNavigation;
        [_locationManager startUpdatingLocation];
     }
    

    3. App gets updatedLocations as normal with "locationManager:didUpdateLocations:" callback:

    -(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
    //  store data
        CLLocation *newLocation = [locations lastObject];
        self.userLocation = newLocation;
    
       //tell the centralManager that you want to deferred this updatedLocation
        if (_isBackgroundMode && !_deferringUpdates)
        {
            _deferringUpdates = YES;
            [self.locationManager allowDeferredLocationUpdatesUntilTraveled:CLLocationDistanceMax timeout:10];
        }
    }
    

    4. But you should handle the data in then "locationManager:didFinishDeferredUpdatesWithError:" callback for your purpose

    - (void) locationManager:(CLLocationManager *)manager didFinishDeferredUpdatesWithError:(NSError *)error {
    
         _deferringUpdates = NO;
    
         //do something 
    }
    

    5. NOTE: I think we should reset parameters of LocationManager each time app switches between background/forground mode.

提交回复
热议问题