paleksandrs / APScheduledLocationManager

MIT License
84 stars 19 forks source link

APScheduledLocationManager

license MIT

Location manager that allows to get background location updates every n seconds with desired location accuracy.

n - range is between 2 - 170 seconds (limited by max allowed background task time)

Requirements

Installation

CocoaPods. Please use the latest CocoaPods as this framework is using Swift 3
use_frameworks!

pod 'APScheduledLocationManager'

Usage

1. Configure Xcode project

In target Capabilities enable Background Modes and check Location updates

In Info.plist add NSLocationAlwaysUsageDescription key and value that will specify the reason for your app to access the user’s location information at all times.

2. Import APScheduledLocationManager and CoreLocation
import APScheduledLocationManager
import CoreLocation
3. Create instance of APScheduledLocationManager
let manager = APScheduledLocationManager(delegate: self)
4. Confirm to APScheduledLocationManagerDelegate
func scheduledLocationManager(_ manager: APScheduledLocationManager, didFailWithError error: Error){

}

func scheduledLocationManager(_ manager: APScheduledLocationManager, didUpdateLocations locations: [CLLocation]) {

}

func scheduledLocationManager(_ manager: APScheduledLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {

}
5. Request authorized always permission

You can use requestAlwaysAuthorization helper method of APScheduledLocationManager or your own implementation.

6. Start location manager when permission is granted
manager.startUpdatingLocation(interval: 170, acceptableLocationAccuracy: 100)

There are 2 options to adjust the location manager:

7. To stop location manager call stopLocationManager

Example

See an example app ScheduledLocationExample in the repository

Note, if you test on a stimulater edit scheme and set default location.

License

APScheduledLocationManager is released under an MIT License. See LICENSE for details.