This library provides the possibility to read the user location even if the app is in background.
ios-location-listener is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'ios-location-listener'
In order to use this library you must:
-
Modify the Info.plist file as follows: a) add a new element key[NSLocationAlwaysAndWhenInUseUsageDescription] = value["Why you need to always access location"] b) add a new element key[NSLocationWhenInUseUsageDescription] = value["Why you need to always access location"]
-
Select your project, select the "Signing & Capabilities" tab, click on the "+" button and add "Background Modes". Check and activate "Location updates", "Backgroud Fetch" and "Background Processing".
-
Verify that the user has the GPS service active.
-
Inside your project, create an instance of the class StreamLocation and a var of the type AnyCancellable.
let stream = StreamLocation()
@State var cancellable: AnyCancellable? = nil
let publisher = stream.subject
- When you want to start updating the stream of the locations, simply call startUpdatingLocations on stream and read from the stream, as follows:
stream.startUpdatingLocations()
DispatchQueue.main.async{
self.cancellable = publisher?.sink{
s in
print("\(s.coordinate.latitude)-\(s.coordinate.longitude)")
}
}
NOTE: this will remain active even when the app is terminated unless you force closed the stream. If the app is terminated the location will be monitored every 150mt approx. - the minimum refresh is decided by iOS.
- When you want to stop updating the stream of locations, call the method stopUpdates on stream and cancel the stream do as follows:
stream.stopUpdates()
DispatchQueue.main.async {
self.cancellable?.cancel()
}
- By default the message in the UserNotification will be "Last position lat:(currentLat), long: (currentLong)". If you want to personalize it, you can call the method setAlertMessage on the stream and pass a string with the custom message you want to be displayed.
Kuama Dev Team with ✌️
ios-location-listener is available under the MIT license. See the LICENSE file for more info.