Delegate method didUpdateLocations is called after trying to use data that has not yet been retrieved from itself - Xcode Swift - ios

I have an NS object (called GoogleSearch) that I use to get the user's location data. These are some global variables created and the init function:
let locationManager = CLLocationManager()
var coordinates: CLLocationCoordinate2D?
override init() {
super.init()
print("1")
DispatchQueue.main.async {
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
if CLLocationManager.locationServicesEnabled() {
self.locationManager.startUpdatingLocation()
}
}
}
Next, here is my delegate method:
extension GoogleSearch: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("2")
let userLocation: CLLocation = locations[0] as CLLocation
self.coordinates = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
}
}
Next, I try to access the coordinates (set in the delegate method) in a later function:
func searchForPlaces(completion: #escaping ([place])->()) {
print(coordinates)
}
Finally, in the ViewController that I implement this NSobject, I have the following code in viewDidLoad:
self.googleSearch = GoogleSearch()
DispatchQueue.main.async {
self.googleSearch.searchForPlaces(completion: { (places) in
DispatchQueue.main.async {
self.places = places
self.placesCollectionView.reloadData()
}
})
}
The problem is that printing coordinates in the searchForPlaces functions prints nil because it is run before the delegate method is called. Is there anything I should change in my NSObject or perhaps my ViewController to ensure that I can access coordinates from searchForPlaces() ?
Thank you.

You can achieve in this way:
class GoogleSearch:NSObject {
let locationManager = CLLocationManager()
var coordinates: CLLocationCoordinate2D?
var completionHandler: ((CLLocationCoordinate2D) -> Void)?
override init() {
super.init()
print("1")
}
func searchForPlaces(completion: #escaping (CLLocationCoordinate2D) -> Void) {
self.completionHandler = completion
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
if CLLocationManager.locationServicesEnabled() {
self.locationManager.stopUpdatingLocation()
self.locationManager.startUpdatingLocation()
}
}
}
extension GoogleSearch: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("2")
let userLocation: CLLocation = locations[0] as CLLocation
self.coordinates = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
self.completionHandler?(self.coordinates!)
}
}

Related

Core Location in MVP

In my project I have a LocationService class that conforms to CLLocationManagerDelegate protocol in order to detect current user's location.
class LocationService: NSObject, CLLocationManagerDelegate {
fileprivate let locationManager = CLLocationManager()
var location: Location? // Location(lat: Double, lon: Double)
override init() {
super.init()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
}
func getCurrentLocation() -> Location? {
locationManager.startUpdatingLocation()
// how can I catch a location?
return location
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
if location.horizontalAccuracy > 0 {
locationManager.stopUpdatingLocation()
self.location = Location(lat: location.coordinate.latitude, lon: location.coordinate.latitude)
}
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error.localizedDescription)
}
}
I want my WeatherPresenter to trigger location updates in LocationService and get the result as soon as location is found. Is there any way to do this?
class WeatherPresenter {
unowned let delegate: WeatherViewDelegate
let weatherService = WeatherService()
let locationService = LocationService()
init(with delegate: WeatherViewDelegate) {
self.delegate = delegate
}
func getWeatherForCurrentLocation() {
if let location = locationService.getCurrentLocation() {
//...
}
}
}
You can use Delegate to notify WeatherPresenter on changes from LocationService
protocol LocationServiceDelegate: class { // Delegate protocol
func didUpdateLocation()
}
class LocationService: NSObject, CLLocationManagerDelegate {
weak var delegate: LocationServiceDelegate?
fileprivate let locationManager = CLLocationManager()
var location: Location? // Location(lat: Double, lon: Double)
override init() {
super.init()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
}
func startUpdatingLocation() { // Start updating called from presenter
locationManager.startUpdatingLocation()
}
func getCurrentLocation() -> Location? {
// how can I catch a location?
return location
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
if location.horizontalAccuracy > 0 {
locationManager.stopUpdatingLocation()
self.location = Location(lat: location.coordinate.latitude, lon: location.coordinate.latitude)
self.delegate?.didUpdateLocation() // Notify delegate on change
}
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error.localizedDescription)
}
}
class WeatherPresenter: LocationServiceDelegate {
unowned let delegate: WeatherViewDelegate
let weatherService = WeatherService()
let locationService = LocationService()
init(with delegate: WeatherViewDelegate) {
self.delegate = delegate
self.locationService.delegate = self // Set self as delegate
self.locationService.startUpdatingLocation() // Requests start updating location
}
func didUpdateLocation() { // This will be called on location change
self.getWeatherForCurrentLocation()
}
func getWeatherForCurrentLocation() {
if let location = locationService.getCurrentLocation() {
//...
}
}
}

iOS - CLLocation Manager didUpdateLocations being called in one class but not another?

Hi I'm making a program that gets the users location and puts an according annotation on the map. I started by writing all of the code in the View Controller and it gets the location perfectly. Below is the working code in the view controller.
class MapViewController: UIViewController, CLLocationManagerDelegate {
var annotation = MKPointAnnotation()
var userLocation: CLLocation?
#IBOutlet weak var mapView: MKMapView!
var locationManager:CLLocationManager!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
determineCurrentLocation()
}
func determineCurrentLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
userLocation = locations[0] as CLLocation
print("user latitude = \(userLocation?.coordinate.latitude)")
print("user longitude = \(userLocation?.coordinate.longitude)")
annotation.coordinate = CLLocationCoordinate2D(latitude: (userLocation?.coordinate.latitude)!, longitude: (userLocation?.coordinate.longitude)!)
annotation.title = "You"
mapView.addAnnotation(annotation)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Error \(error)")
}
However now when I try and recreate almost the exact same code in another swift file. didUpdateLocations never gets called. locationManager.startUpdatingLocation() does get called.
Below is my new swift file which I call from the View Controller. Is there any simple concept I'm missing here because I really don't see why this doesn't work.
import Foundation
import CoreLocation
class SendLocation: NSObject, CLLocationManagerDelegate {
var userLocation: CLLocation?
var locationManager:CLLocationManager!
func sendLocationPost() {
determineCurrentLocation()
}
func determineCurrentLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
print("WHY")
if CLLocationManager.locationServicesEnabled(){
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
userLocation = locations[0] as CLLocation
print("user latitude = \(userLocation?.coordinate.latitude)")
print("user longitude = \(userLocation?.coordinate.longitude)")
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Error \(error)")
}
}
I call it using :
let location = SendLocation()
location.sendLocationPost()`
in my View Controller
This happens because you are not keeping a reference to your SendLocation object.
Make SendLocation a property of your UIViewController.
For example, calling it from a static scope will not keep a reference.
WONT WORK:
static func sendLocation() {
let location = SendLocation()
location.sendLocationPost()
}
WILL WORK
let location = SendLocation()
func sendLocation() {
location.sendLocationPost()
}

iOS geolocation, Code works differently when initialization and declaration happen together

In iOS geolocation, the code works if the locationManager is declared and initialized separately, however it does not work if it is declared and initialized at the same time. Why is it so? The following is the working code sample:-
var locationManager : CLLocationManager!
func initLocManager() {
locationManager=CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.activityType = .automotiveNavigation
locationManager.distanceFilter = 10.0
}
func retrieveLocation(){
initLocManager()
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
for location in locations {
print("Long \(location.coordinate.longitude)")
print("Lati \(location.coordinate.latitude)")
}
}
whereas the following code does not work:-
var locationManager = CLLocationManager()
func initLocManager() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.activityType = .automotiveNavigation
locationManager.distanceFilter = 10.0
}
func retrieveLocation(){
initLocManager()
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
for location in locations {
print("Long \(location.coordinate.longitude)")
print("Lati \(location.coordinate.latitude)")
}
}
Another way:
Create new file with this code:
import UIKit
import CoreLocation
class LocationManager: CLLocationManager, CLLocationManagerDelegate {
static let shared = LocationManager()
public var currentLocation = CLLocation() {
didSet {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: LocationManager.LocationUpdatedNotification), object: self, userInfo: nil)
}
}
static let LocationUpdatedNotification: String = "LocationUpdate"
private override init() {
super.init()
self.delegate = self
self.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
self.activityType = .automotiveNavigation
self.distanceFilter = 10.0
self.requestAlwaysAuthorization()
self.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let lastLocation = locations.last {
currentLocation = lastLocation
}
}
}
In AppDelegate.swift
_ = LocationManager.shared // Add this line to func didFinishLaunchingWithOptions
Now you can get current user location using this code:
LocationManager.shared.currentLocation
Also you can subscribe for LocationUpdate notification anywhere in your project.

CLLocationManager delegate methods are not getting called(google maps is integrated)

The delegate Methods of CLLocationManager
didChangeAuthorizationStatus
and
didUpdateToLocation
are not getting called.
Location Always Usage Description key is already added in info.plist and I am getting notification also when i launch app for the first time.
I am able to see the google map, but i am not able to see current location, When i change location, its not getting updated. Basicaaly delegate methods are not getting called.
//code
import UIKit
import GoogleMaps
class ViewController: UIViewController,GMSMapViewDelegate {
#IBOutlet weak var mapViewTest: GMSMapView!
let locationManager = CLLocationManager()
var currentLocation :CLLocation = CLLocation(latitude: 0.0, longitude: 0.0)
var currentLatitude : Double = 0.0
var currentLongitude : Double = 0.0
override func viewDidLoad()
{
super.viewDidLoad()``
locationManager.delegate = self
if (CLLocationManager.locationServicesEnabled())
{
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.allowsBackgroundLocationUpdates = true
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
// Do any additional setup after loading the view, typically from a nib.
}
}
extension ViewController : CLLocationManagerDelegate
{
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus)
{
if status == .authorizedAlways
{
if(CLLocationManager .locationServicesEnabled())
{
locationManager.startUpdatingLocation()
mapViewTest.isMyLocationEnabled = true
mapViewTest.settings.myLocationButton = true
}
}
}
func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation)
{
mapViewTest.camera = GMSCameraPosition(target: (newLocation.coordinate), zoom: 15, bearing: 0, viewingAngle: 0)
currentLocation = newLocation
currentLatitude = newLocation.coordinate.latitude
currentLongitude = newLocation.coordinate.longitude
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Errors: " + error.localizedDescription)
}
}
From your code you are working with Swift 3, and in Swift 3 CLLocationManagerDelegate method's signature is changed like this.
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
}
//func locationManager(_ manager: CLLocationManager, didUpdateTo newLocation: CLLocation,
from oldLocation: CLLocation) is deprecated with below one
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
}
Check Apple Documentation on CLLocationManagerDelegate for more details.
After checking your code i found some changes you needs to do as follow,
note : I have only added the code having issue here that of location manager
import UIKit
import CoreLocation
class ViewController: UIViewController {
let locationManager = CLLocationManager()
var currentLocation :CLLocation = CLLocation(latitude: 0.0, longitude: 0.0)
var currentLatitude : Double = 0.0
var currentLongitude : Double = 0.0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
if (CLLocationManager.locationServicesEnabled())
{
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.allowsBackgroundLocationUpdates = true
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController : CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
}
func locationManager(_ manager: CLLocationManager, didFinishDeferredUpdatesWithError error: Error?) {
print("Errors: " + (error?.localizedDescription)!)
}
}
Also add below lines in .plist file if not added,
<key>NSLocationWhenInUseUsageDescription</key>
<string>$(PRODUCT_NAME) location use</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>$(PRODUCT_NAME) always uses location </string>
Add these two properties in your info.plist
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
you need to put the mapview delegate to self.
try this:
override func viewDidLoad() {
super.viewDidLoad()
// User Location Settings
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
// Google Maps Delegate
mapView.delegate = self
}
// View will appear
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Google maps settings
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
// Get location if autorized
if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse) {
let (latitude, longitude) = self.getLocation()
mapView.camera = GMSCameraPosition.camera(
withLatitude: latitude,
longitude: longitude,
zoom: 14)
}
}
//Get the user location
func getLocation() -> (latitude: CLLocationDegrees, longitude: CLLocationDegrees) {
let latitude = (locationManager.location?.coordinate.latitude)!
let longitude = (locationManager.location?.coordinate.longitude)!
return (latitude, longitude)
}

how to wait till locaiton manager delegate called in swift?

i have a general function genParam() as soon as i called this method , i want to return the current userGPS location along with other parameters.but in my case genParam() returning immediately before calling the delegate method didUpdateLocations.Is there any way to wait till delegate method gets called before returning genParam().
class CommonApiParamGenerator: NSObject,CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var userGPSLoc:String = ""
func genParam(locationName:String)->NSMutableDictionary{
self.getUserLocation()
let guid = NSUUID().UUIDString
let userName = SingleTon.sharedInstance.getUserName()
let gpsLoc = self.userGPSLoc
let commonParam = NSMutableDictionary(objects: [guid,userName,gpsLoc], forKeys: ["guid","userName","gpsLoc"])
return commonParam
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let userLocation:CLLocation = locations[0] as CLLocation
self.userGPSLoc = "\(userLocation.coordinate.latitude),\(userLocation.coordinate.longitude)"
print("receivedGPS \(self.userGPSLoc)")
manager.stopUpdatingLocation()
}
func getUserLocation()
{
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
}
Firstly accroding to single responsibility principle I propose you to move you code with getting location in separate class:
class LocationManager: NSObject, CLLocationManagerDelegate {
let manager = CLLocationManager()
private var completion: ((CLLocation) -> Void)?
override init() {
super.init()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
}
func getLocation(completion: (CLLocation) -> Void) {
self.completion = completion
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let userLocation = locations[0] as CLLocation
manager.stopUpdatingLocation()
self.completion?(userLocation)
}
}
Then refactor you class:
class CommonApiParamGenerator: NSObject {
var userGPSLoc:String = ""
var locationManager = LocationManager()
func genParam(locationName:String, completion: (NSMutableDictionary) -> Void) {
let guid = NSUUID().UUIDString
let userName = SingleTon.sharedInstance.getUserName()
locationManager.getLocation { location in
let userGPSLoc = "\(location.coordinate.latitude),\(location.coordinate.longitude)"
completion(NSMutableDictionary(objects: [guid,userName,userGPSLoc], forKeys: ["guid","userName","gpsLoc"]))
}
}
}
Usage:
let generator = CommonApiParamGenerator()
generator.genParam(locationName) { params in
...
}
Main idea is using callbacks for asynchronous operations.
If you don't like callbacks, or there are a lot of nested callbacks (callback hell) you can you PromiseKit: http://promisekit.org/.
Good luck!

Resources