Periodic iOS background location updates

前端 未结 9 1443
佛祖请我去吃肉
佛祖请我去吃肉 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:27

    To use standard location services while the application is in the background you need first turn on Background Modes in the Capabilities tab of the Target settings, and select Location updates.

    Or, add it directly to the Info.plist.

    NSLocationAlwaysUsageDescription
     I want to get your location Information in background
    UIBackgroundModes
     location 
    

    Then you need to setup the CLLocationManager

    Objective C

    //The Location Manager must have a strong reference to it.
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate = self;
    //Request Always authorization (iOS8+)
    if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { [_locationManager requestAlwaysAuthorization];
    }
    //Allow location updates in the background (iOS9+)
    if ([_locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]) { _locationManager.allowsBackgroundLocationUpdates = YES;
    }
    [_locationManager startUpdatingLocation];
    

    Swift

    self.locationManager.delegate = self
    if #available (iOS 8.0,*) {
        self.locationManager.requestAlwaysAuthorization()
    }
    if #available (iOS 9.0,*) {
        self.locationManager.allowsBackgroundLocationUpdates = true
    }
    self.locationManager.startUpdatingLocation()
    
    

    Ref:https://medium.com/@javedmultani16/location-service-in-the-background-ios-942c89cd99ba

提交回复
热议问题