How to track user location in background?

前端 未结 8 951
梦如初夏
梦如初夏 2020-12-12 23:27

I\'m looking for an open source app or library to track user location in the background. Now I\'m trying to do it with CLLocation and background tasks, but accu

相关标签:
8条回答
  • 2020-12-13 00:27

    So your app uses location services. Then please read the Location Awareness Programming Guide.

    You need to make some changes to your Info.plist:

    • If your app relies on location services to function properly, add location-services to UIRequiredDeviceCapabilities
    • if your app requires GPS hardware, add gps to UIRequiredDeviceCapabilities
    • if you need to run your app longer then 10 minutes in the background, add location to UIBackgroundModes. Then your location manager will deliver locations beyond the 10-minute-limit.
    • you should also set NSLocationUsageDescription (can also be localized)

    Getting Location Events in the Background

    If your app needs location updates delivered whether the app is in the foreground or background, there are multiple options for doing so. The preferred option is to use the significant location change service to wake your app at appropriate times to handle new events. However, if your app needs to use the standard location service, you can declare your app as needing background location services.

    An app should request background location services only if the absence of those services would impair its ability to operate. In addition, any app that requests background location services should use those services to provide a tangible benefit to the user. For example, a turn-by-turn navigation app would be a likely candidate for background location services because of its need to track the user’s position and report when it is time to make the next turn.

    0 讨论(0)
  • 2020-12-13 00:30

    You can set up monitoring location stuff in you VC as below

    in viewDidLoad method do as below

    CLLocationManager locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;(Accuracy according to your need)
    [locationManager startUpdatingLocation];
    

    than you have to overrite below two optional delegate methods of CLLocationManagerDelegate protocol

    for iOS6+

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{}
    

    and for iOS 2 to 6

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

    in these methods you will get updated location. use it as you want. every time location updated these method get calls.

    0 讨论(0)
提交回复
热议问题