Trckr

public class Trckr : NSObject, CLLocationManagerDelegate

Provides location tracking using a combination of:

  • Continuous location updates (while the user is moving)
  • Significant location changes (to sustain background operation)
  • Region monitoring, using Geofence (when the user is stationary)
  • Beacon region monitoring (e.g. detecting vehicle used)

Usage:

    // start tracking
    Trckr.sharedInstance.trackingEnabled = true

    // stop tracking
    Trckr.sharedInstance.trackingEnabled = false

When using Trckr, you must add the following entries to your app’s info.plist:

  • NSMotionUsageDescription
  • NSLocationAlwaysAndWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription
  • NSLocationWhenInUseUsageDescription
  • UIBackgroundModes: location
  • This is the one and only Trckr instance

    Declaration

    Swift

    @objc
    public static let shared: Trckr
  • location updates and other events are delivered to this delegate.

    Declaration

    Swift

    @objc
    public var delegate: TrckrDelegate?
  • Locations received from the GPS are stored in this array.

    This buffer stored locations received within the last Trckr.StationaryInterval seconds. Hence, you should not rely on locations as storage because it automatically removes old locations.

    The purpose of locations buffer is to determine when the user becomes stationary. It is used as the default value for locations param in stationary(locations:interval:after:). When the user becomes stationary, continuous location tracking stops.

    You may increase the number of locations stored in this buffer by setting Trckr.StationaryInterval to a larger value. Remember, however, that this buffer is stored in memory does not persist after your app is terminated.

    Declaration

    Swift

    public var locations: [CLLocation] { get }
  • The last location received from CoreLocation. This is effectively the last known location of the user.

    Declaration

    Swift

    public var lastLocation: CLLocation? { get }
  • Start / Stop location tracking. Setting this variable to true for the first time will prompt the user to allow access to location services. Note: Trckr requires Always permission in order to track location in the background. If the user becomes stationary, Trckr will automatically turn off continuous location updates and rely on geofencing and/or significant location monitoring in order to save power. Continuous location tracking will then resume when the user starts moving again, which will be signaled via geofence EXIT event. This event may be triggered between 150 - 1000 meters away from the geofence center.

    Declaration

    Swift

    public var trackingEnabled: Bool { get set }
  • *true* if location services are available (enabled) on the device and `authorizationStatus == .authorizeAlways`
    Trckr requires *always* location authorization in order to track locations in the background
    
    Note: your app must include the following in its info.plist:
    
    ```
    

    UIBackgroundModes location “`

    Declaration

    Swift

    public var locationServicesEnabled: Bool { get }
  • The minimum acceptable accuracy, in meters. The higher this value, the lower the accuracy. This value is used to filter new incoming locations from CoreLocation.

    Default: 200 meters

    Declaration

    Swift

    public static let DesiredAccuracy: CLLocationAccuracy
  • The user is considered to be non-stationary if moved more than this distance (in meters) during the last StationaryInterval Default value: 150 meters

    Declaration

    Swift

    public static var StationaryDistance: CLLocationDistance
  • In order to evaluate if the user is stationary or not, the average speed measured during the last StationaryInterval seconds must be lower than StationarySpeed. Default value: 120 secs

    See

    see also: StationarySpeed

    Declaration

    Swift

    public static var StationaryInterval: TimeInterval
  • The user is considered to be stationary if the average speed measured with locations received in the last StationaryInterval seconds is less than this value. Default value: 0.85 m/s

    Declaration

    Swift

    public static var StationarySpeed: CLLocationSpeed
  • Set a tracking schedule to limit location tracking to specific days/times. This variable takes crontab syntax and currently supports hours and days only.

    Example:

    Limit location tracking to Mon, Tue, Wed, Thu, Fri between 10:00 and 20:00 hours.

    trackingEnabled = "* 10-20 * * 1,2,3,4,5"
    

    More info about crontab syntax: https://crontab.guru/

    When this variable is set, you can still allow tracking outside the schedule by implementing TrckrDelegate.shouldTrackOutsideSchedule()

    Declaration

    Swift

    public var trackingSchedule: String? { get set }
  • the tag to use for trips if user is out of schedule

    Declaration

    Swift

    public var tagForOffScheduleTracking: String? { get set }
  • Check whether tracking is allowed right now.

    This variable evaluates to true if the current time is within the trackingSchedule, or if no trackingSchedule is set.

    Declaration

    Swift

    public var isWithinSchedule: Bool { get }
  • Determine if an array of locations represents stationary movement at a specific time interval. If after is provided, only locations with timestamp later than after will be considered.

    Declaration

    Swift

    public class func stationary(locations: [CLLocation] = Trckr.shared.locations, interval: TimeInterval = Trckr.StationaryInterval, after: Date? = nil) -> CLLocation?

    Parameters

    locations

    the locations array to evaluate.

    interval

    locations will be collected based on their timestamp, until interval is satisfied, and then evaluated based on bounding box.

    after

    ignore locations before this timestamp. Pass nil to consider all locations (until interval is met).

    Return Value

    the stationary location from an array, if found