Geolocation native iOS library ⚡️
https://github.com/ionic-team/ion-ios-geolocation.git
A Swift library for iOS that provides simple, reliable access to device GPS capabilities. Get location data, monitor position changes, and manage location services with a clean, modern API.
ion-ios-geolocation is available through CocoaPods. Add this to your Podfile:
pod 'IONGeolocationLib', '~> 2.1.0'
This library is currently used by the Geolocation Plugin for OutSystems' Cordova and Capacitor Plugins. Please check the library usage there for real use-case scenarios.
All the library's features are split in 4 different protocols. Each are detailed in the following subsections:
IONGLOCServicesCheckerIONGLOCAuthorisationHandlerIONGLOCSingleLocationHandlerIONGLOCMonitorLocationHandlerIONGLOCRequestOptionsModelIONGLOCService that merges all protocols together. Its concrete implementation is achieved by the IONGLOCManagerWrapper class.
IONGLOCServicesCheckerThe sole goal of IONGLOCServicesChecker is to verify if the location services have been enabled on the device.
func areLocationServicesEnabled() -> Bool
Returns a Boolean value indicating whether location services are enabled on the device.
IONGLOCAuthorisationHandlerManages all authorisation status logic related with location. It's composed by the following:
var authorisationStatus: IONGLOCAuthorisation
It returns the at-the-moment authorisation status to use the device's location services. The following are the possible values:
notDetermined: User hasn't chosen whether the app can use location services. This is the property's default value;restricted: App is not authorized to use location services;denied: User denied the use of location services for the app or globally;authorisedAlways: User authorized the app to start location services at any time;authorisedWhenInUse: User authorized the app to start location services while it is in use.var authorisationStatusPublisher: Published<IONGLOCAuthorisation>.Publisher
It returns a publisher that delivers all authorisation status updates to whoever subscribes to it. The authorisationStatus values are the elements that can be emitted by authorisationStatusPublisher.
func requestAuthorisation(withType authorisationType: IONGLOCAuthorisationRequestType)
Requests the user’s permission to use location services. There are two types of authorisation that can be requested:
always: Requests the user’s permission to use location services regardless of whether the app is in use;whenInUse: Requests the user’s permission to use location services while the app is in use.IONGLOCLocationHandlerManages all location-related information. It's composed by the following:
nil if there hasn't been a request or in case of some issue occurring while fetching it;IONGLOCLocationHandler serves has the base for both IONGLOCSingleLocationHandler and IONGLOCMonitorLocationHandler. More on both later.
var currentLocation: IONGLOCPositionModel?
It returns the device's latest fetched location position. It can be nil if there hasn't been a request or in case of some issue occuring while fetching it.
IONGLOCPositionModel is composed by the following properties:
altitude: Altitude above mean sea level, measured in meters (m);course: Direction in which the device is travelling, measured in degrees (º) and relative to due north;horizontalAccuracy: Radius of uncertainty, measured in meters (m);latitude: Latitude of the geographical coordinate, measured in degrees (º) and relative to due north;longitude: Longitude of the geographical coordinate, measured in degrees (º) and relative to the zero meridian;speed: Instantaneous speed of the device, measured in meters per second (m/s);timestamp: Time at which this location was determined, measured in milliseconds (ms) elapsed since the UNIX epoch (Jan 1, 1970);verticalAccuracy: Validity of the altitude values and their estimated uncertainty, measured in meters (m).magneticHeading: The heading (measured in degrees) relative to magnetic north.trueHeading: The heading (measured in degrees) relative to true north.headingAccuracy: The maximum deviation (measured in degrees) between the reported heading and the true geomagnetic heading.var currentLocationPublisher: AnyPublisher<IONGLOCPositionModel, IONGLOCLocationError>
It returns a publisher that delivers all location updates to whoever subscribes to it. The currentLocation values are the elements that can be emitted by currentLocationPublisher.
var locationTimeoutPublisher: AnyPublisher<IONGLOCLocationError, Never>
It returns a publisher that emits a .timeout event when a request exceeds the specified timeout in IONGLOCRequestOptionsModel.
func updateConfiguration(_ configuration: IONGLOCConfigurationModel)
Updates two properties that condition how location update events are generated:
enableHighAccuracy: Boolean value that indicates if the app wants location data accuracy to be at its best or not. It needs to be explicitly mentioned by the method callersminimumUpdateDistanceInMeters: Minimum distance the device must move horizontally before an update event is generated, measured in meters (m). As it's optional, it can be omitted by the method callers.IONGLOCRequestOptionsModelUsed to configure options for location requests.
timeout: Maximum duration (ms) to wait for a location update. Default is 5000. let options = IONGLOCRequestOptionsModel(timeout: 10000)
// Single location
locationService.requestSingleLocation(options: options)
// Continuous monitoring
locationService.startMonitoringLocation(options: options)
IONGLOCSingleLocationHandlerIt's responsible to trigger one-time deliveries of the device's current location. It's composed by the following:
func requestSingleLocation(options: IONGLOCRequestOptionsModel)
The method returns immediately. By calling it, it triggers an update to currentLocation and a new element delivery by currentLocationPublisher.
Note: The signature of requestSingleLocation has changed.
You now need to pass an IONGLOCRequestOptionsModel to configure options such as timeout.
IONGLOCMonitorLocationHandlerIt's responsible for the continuous generation of updates that report the device's current location position. It's composed by the following:
func startMonitoringLocation(options: IONGLOCRequestOptionsModel)
func startMonitoringLocation()
currentLocation and signal currentLocationPublisher to continuously emit relevant location updates.
func stopMonitoringLocation()
The method should be called whenever you no longer need to receive location-related events.
The library uses IONGLOCLocationError for error handling regarding location position updates. Possible errors include:
enum IONGLOCLocationError: Error {
case locationUnavailable
case timeout
case other(_ error: Error)
}
Location updates are delivered as IONGLOCPositionModel objects:
{
"latitude": 37.7749,
"longitude": -122.4194,
"altitude": 0.0,
"horizontalAccuracy": 5.0,
"verticalAccuracy": 10.0,
"course": 180.0,
"speed": 0.0,
"timestamp": 1641034800000,
"magneticHeading": 5.0,
"trueHeading": 5.0,
"headingAccuracy": 0.0
}
enableHighAccuracy: true) uses GPS and significantly impacts battery lifeminimumUpdateDistanceInMeters for battery optimizationstopMonitoringLocation() when updates are no longer neededTo enable background location updates:
Info.plist:UIBackgroundModes
Location updates
locationService.requestAuthorisation(withType: .always)
Common issues and solutions:
Info.plist permissionsgit checkout -b feature/amazing-feature)git commit -m 'Add amazing feature')git push origin feature/amazing-feature)ion-ios-geolocation is available under the MIT license. See the LICENSE file for more info.