|
1 | | -# OSGeolocationLib-iOS |
| 1 | +# OSGeolocationLib |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +[](https://cocoapods.org/pods/OSGeolocationLib) |
| 6 | +[](https://cocoapods.org/pods/OSGeolocationLib) |
| 7 | +[](https://cocoapods.org/pods/OSGeolocationLib) |
| 8 | + |
| 9 | +## Requirements |
| 10 | + |
| 11 | +- iOS 14.0+ |
| 12 | +- Swift 5.0+ |
| 13 | +- Xcode 15.0+ |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +### CocoaPods |
| 18 | + |
| 19 | +`OSGeolocationLib` is available through [CocoaPods](https://cocoapods.org). Add this to your Podfile: |
| 20 | + |
| 21 | +```ruby |
| 22 | +pod 'OSGeolocationLib', '~> 1.0.0' # Use the latest 1.0.x version |
| 23 | +``` |
| 24 | + |
| 25 | +## Features |
| 26 | + |
| 27 | +All the library's features are split in 4 different protocols. Each are detailed in the following subsections: |
| 28 | +- `OSGLOCServicesChecker` |
| 29 | +- `OSGLOCAuthorisationHandler` |
| 30 | +- `OSGLOCSingleLocationHandler` |
| 31 | +- `OSGLOCMonitorLocationHandler` |
| 32 | + |
| 33 | +There's also the typealias `OSGLOCService` that merges all protocols together. Its concrete implementation is achieved by the `OSGLOCManagerWrapper` class. |
| 34 | + |
| 35 | +### `OSGLOCServicesChecker` |
| 36 | + |
| 37 | +The sole goal of `OSGLOCServicesChecker` is to verify if the location services have been enabled on the device. |
| 38 | + |
| 39 | +#### Check if Location Services are Enabled |
| 40 | + |
| 41 | +```swift |
| 42 | +func areLocationServicesEnabled() -> Bool |
| 43 | +``` |
| 44 | + |
| 45 | +Returns a Boolean value indicating whether location services are enabled on the device. |
| 46 | + |
| 47 | + |
| 48 | +### `OSGLOCAuthorisationHandler` |
| 49 | + |
| 50 | +Manages all authorisation status logic related with location. It's composed by the following: |
| 51 | +- a property that indicates the app's at-the-moment authorisation status to use location services; |
| 52 | +- a publisher that delivers all authorisation status updates to its subscribers; |
| 53 | +- a method that requests the user's permission to use location services. |
| 54 | + |
| 55 | +Authorisation is vital to receive location-related information. The user needs to be prompted to grant permission to the app to use location services. |
| 56 | + |
| 57 | +#### Location Services' Authorisation Status Property |
| 58 | + |
| 59 | +```swift |
| 60 | +var authorisationStatus: OSGLOCAuthorisation |
| 61 | +``` |
| 62 | + |
| 63 | +It returns the at-the-moment authorisation status to use the device's location services. The following are the possible values: |
| 64 | +- `notDetermined`: User hasn't chosen whether the app can use location services. This is the property's default value; |
| 65 | +- `restricted`: App is not authorized to use location services; |
| 66 | +- `denied`: User denied the use of location services for the app or globally; |
| 67 | +- `authorisedAlways`: User authorized the app to start location services at any time; |
| 68 | +- `authorisedWhenInUse`: User authorized the app to start location services while it is in use. |
| 69 | + |
| 70 | +#### Location Services' Authorisation Status Publisher |
| 71 | + |
| 72 | +```swift |
| 73 | +var authorisationStatusPublisher: Published<OSGLOCAuthorisation>.Publisher |
| 74 | +``` |
| 75 | + |
| 76 | +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`. |
| 77 | + |
| 78 | +#### Request User's Permission to Use Location Services |
| 79 | + |
| 80 | +``` |
| 81 | +func requestAuthorisation(withType authorisationType: OSGLOCAuthorisationRequestType) |
| 82 | +``` |
| 83 | + |
| 84 | +Requests the user’s permission to use location services. There are two types of authorisation that can be requested: |
| 85 | +- `always`: Requests the user’s permission to use location services regardless of whether the app is in use; |
| 86 | +- `whenInUse`: Requests the user’s permission to use location services while the app is in use. |
| 87 | + |
| 88 | +### `OSGLOCLocationHandler` |
| 89 | + |
| 90 | +Manages all location-related information. It's composed by the following: |
| 91 | +- a property that retrieves the device's at-the-moment location position. It can be `nil` if there hasn't been a request or in case of some issue occurring while fetching it; |
| 92 | +- a publisher that delivers all location updates to its subscribers. This includes successful updates or the error it occurred while updating. |
| 93 | +- a method that updates two conditions that influence how the location updates are performed: |
| 94 | + - the location data accuracy the app wants to receive; |
| 95 | + - the minimum distance the device must move horizontally before an update event is generated. The distance is measured in meters (m). |
| 96 | + |
| 97 | +`OSGLOCLocationHandler` serves has the base for both `OSGLOCSingleLocationHandler` and `OSGLOCMonitorLocationHandler`. More on both later. |
| 98 | + |
| 99 | +#### Current Location Property |
| 100 | + |
| 101 | +```swift |
| 102 | +var currentLocation: OSGLOCPositionModel? |
| 103 | +``` |
| 104 | + |
| 105 | +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. |
| 106 | +`OSGLOCPositionModel` is composed by the following properties: |
| 107 | +- `altitude`: Altitude above mean sea level, measured in meters (m); |
| 108 | +- `course`: Direction in which the device is travelling, measured in degrees (º) and relative to due north; |
| 109 | +- `horizontalAccuracy`: Radius of uncertainty, measured in meters (m); |
| 110 | +- `latitude`: Latitude of the geographical coordinate, measured in degrees (º) and relative to due north; |
| 111 | +- `longitude`: Longitude of the geographical coordinate, measured in degrees (º) and relative to the zero meridian; |
| 112 | +- `speed`: Instantaneous speed of the device, measured in meters per second (m/s); |
| 113 | +- `timestamp`: Time at which this location was determined, measured in milliseconds (ms) elapsed since the UNIX epoch (Jan 1, 1970); |
| 114 | +- `verticalAccuracy`: Validity of the altitude values and their estimated uncertainty, measured in meters (m). |
| 115 | + |
| 116 | +#### Current Location Publisher |
| 117 | + |
| 118 | +```swift |
| 119 | +var currentLocationPublisher: AnyPublisher<OSGLOCPositionModel, OSGLOCLocationError> |
| 120 | +``` |
| 121 | + |
| 122 | +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`. |
| 123 | + |
| 124 | +#### Update the Location Manager's Configuration |
| 125 | + |
| 126 | +```swift |
| 127 | +func updateConfiguration(_ configuration: OSGLOCConfigurationModel) |
| 128 | +``` |
| 129 | + |
| 130 | +Updates two properties that condition how location update events are generated: |
| 131 | +- `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 callers |
| 132 | +- `minimumUpdateDistanceInMeters`: 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. |
| 133 | + |
| 134 | +### `OSGLOCSingleLocationHandler` |
| 135 | + |
| 136 | +It's responsible to trigger one-time deliveries of the device's current location. It's composed by the following: |
| 137 | +- a method that requests the user's current location position. |
| 138 | + |
| 139 | +#### Request Device's Current Location |
| 140 | + |
| 141 | +```swift |
| 142 | +func requestSingleLocation() |
| 143 | +``` |
| 144 | + |
| 145 | +The method returns immediately. By calling it, it triggers an update to `currentLocation` and a new element delivery by `currentLocationPublisher`. |
| 146 | + |
| 147 | + |
| 148 | +### `OSGLOCMonitorLocationHandler` |
| 149 | + |
| 150 | +It's responsible for the continuous generation of updates that report the device's current location position. It's composed by the following: |
| 151 | +- a method that starts the generation of updates; |
| 152 | +- a method that ends the generation of updates. |
| 153 | + |
| 154 | +#### Start Monitoring the Device's Position |
| 155 | + |
| 156 | +```swift |
| 157 | +func startMonitoringLocation() |
| 158 | +``` |
| 159 | + |
| 160 | +The method returns immediately. By calling it, it triggers an update to `currentLocation` and signals `currentLocationPublisher` to continuously emit relevant location updates. |
| 161 | + |
| 162 | +#### Stop Monitoring the Device's Position |
| 163 | + |
| 164 | +```swift |
| 165 | +func stopMonitoringLocation() |
| 166 | +``` |
| 167 | + |
| 168 | +The method should be called whenever you no longer need to received location-related events. |
| 169 | + |
| 170 | +## Quick Start |
| 171 | + |
| 172 | +This library is currently used by the Geolocation Plugin for OutSystems' [Cordova](https://google.com) and [Capacitor](https://google.com) Plugins. Please check the library usage there for real use-case scenarios. |
| 173 | + |
| 174 | +## Error Handling |
| 175 | + |
| 176 | +The library uses `OSGLOCLocationError` for error handling regarding location position updates. Possible errors include: |
| 177 | + |
| 178 | +```swift |
| 179 | +enum OSGLOCLocationError: Error { |
| 180 | + case locationUnavailable |
| 181 | + case other(_ error: Error) |
| 182 | +} |
| 183 | +``` |
| 184 | + |
| 185 | +## Location Data Format |
| 186 | + |
| 187 | +Location updates are delivered as `OSGLOCPositionModel` objects: |
| 188 | + |
| 189 | +```json |
| 190 | +{ |
| 191 | + "latitude": 37.7749, |
| 192 | + "longitude": -122.4194, |
| 193 | + "altitude": 0.0, |
| 194 | + "horizontalAccuracy": 5.0, |
| 195 | + "verticalAccuracy": 10.0, |
| 196 | + "course": 180.0, |
| 197 | + "speed": 0.0, |
| 198 | + "timestamp": 1641034800000 |
| 199 | +} |
| 200 | +``` |
| 201 | + |
| 202 | +## Battery Impact Considerations |
| 203 | + |
| 204 | +- High accuracy mode (`enableHighAccuracy: true`) uses GPS and significantly impacts battery life |
| 205 | +- Consider using a larger `minimumUpdateDistanceInMeters` for battery optimization |
| 206 | +- Call `stopMonitoringLocation()` when updates are no longer needed |
| 207 | + |
| 208 | +## Background Location |
| 209 | + |
| 210 | +To enable background location updates: |
| 211 | + |
| 212 | +1. Add required background modes to `Info.plist`: |
| 213 | +```xml |
| 214 | +UIBackgroundModes |
| 215 | + Location updates |
| 216 | +``` |
| 217 | + |
| 218 | +2. Request "always" authorization: |
| 219 | + |
| 220 | +```swift |
| 221 | +locationService.requestAuthorisation(withType: .always) |
| 222 | +``` |
| 223 | + |
| 224 | +## Troubleshooting |
| 225 | + |
| 226 | +Common issues and solutions: |
| 227 | + |
| 228 | +1. Location updates not received |
| 229 | + - Check authorization status |
| 230 | + - Verify location services are enabled |
| 231 | + - Ensure proper `Info.plist` permissions |
| 232 | + |
| 233 | +2. Poor accuracy |
| 234 | + - Enable high accuracy mode |
| 235 | + - Ensure clear sky view |
| 236 | + - Wait for better GPS signal |
| 237 | + |
| 238 | +## Contributing |
| 239 | + |
| 240 | +1. Fork the repository |
| 241 | +2. Create your feature branch (`git checkout -b feature/amazing-feature`) |
| 242 | +3. Commit your changes (`git commit -m 'Add amazing feature'`) |
| 243 | +4. Push to the branch (`git push origin feature/amazing-feature`) |
| 244 | +5. Open a Pull Request |
| 245 | + |
| 246 | +## License |
| 247 | + |
| 248 | +`OSGeolocationLib` is available under the MIT license. See the [LICENSE](LICENSE) file for more info. |
| 249 | + |
| 250 | +## Support |
| 251 | + |
| 252 | +- Report issues on our [Issue Tracker](https://github.com/ionic-team/OSGeolocationLib-iOS/issues) |
0 commit comments