didUpdateLocations not being called
This is way late for an answer, but I stumbled across this when I was having the same issue (didChangeAuthorizationStatus
was being called but not didUpdateLocations
). Well of course it's something not code related, but rather I was testing in the simulator which did not have a location set. Thus the location was never found, resulting in didUpdateLocations
never being called.
To fix this... in the simulator go to Debug -> Location-> <choose location>
.
EDIT It should also be noted that locationManager:didFailWithError:
will run if the location is not set in the simulator, as you'd expect.
didUpdateLocations never called
where do you start the location update ? for example:
//location manager
lazy var locationManager: CLLocationManager = {
var _locationManager = CLLocationManager()
_locationManager.delegate = self
_locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
_locationManager.activityType = . automotiveNavigation
_locationManager.distanceFilter = 10.0 // Movement threshold for new events
_locationManager.allowsBackgroundLocationUpdates = true // allow in background
return _locationManager
}()
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation() // start location manager
}
here is a working conroller code:
also important to to set up Custom iOS Target Properties.
Add these two lines to the Info.plist:
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
//
// ViewController.swift
// LocationTest2
import UIKit
import CoreLocation
class ViewController: UIViewController {
//location manager
lazy var locationManager: CLLocationManager = {
var _locationManager = CLLocationManager()
_locationManager.delegate = self
_locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
_locationManager.activityType = . automotiveNavigation
_locationManager.distanceFilter = 10.0 // Movement threshold for new events
// _locationManager.allowsBackgroundLocationUpdates = true // allow in background
return _locationManager
}()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//allow location use
locationManager.requestAlwaysAuthorization()
print("did load")
print(locationManager)
//get current user location for startup
// if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
// }
}
}
// MARK: - CLLocationManagerDelegate
extension ViewController: CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
for location in locations {
print("**********************")
print("Long \(location.coordinate.longitude)")
print("Lati \(location.coordinate.latitude)")
print("Alt \(location.altitude)")
print("Sped \(location.speed)")
print("Accu \(location.horizontalAccuracy)")
print("**********************")
}
}
}
Why is locationManager (:didUpdateLocations) not called
You need to make
let locManager = CLLocationManager()
an instance var to hold a strong reference for the delegate , also you either need 1 of these
locManager.startUpdatingLocation()
locManager.requestLocation()
didUpdateLocations not working in Swift 3
I would reccomend you to move the code that manages the Location to a class like this one. This code is from an app i made a few months ago, i tested it to see if it works and i does. So give it a try. You just need to create an object of this class. I left a commented print in the method locationManager(:didupdate)
, you can uncomment it to see all the changes in you location.
I know it is not perfect but it got me the job done.
import Foundation
import MapKit
import CoreLocation
class Location: CLLocationManager, CLLocationManagerDelegate {
private var latitude:Double
private var longitud:Double
private let locationManager = CLLocationManager()
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
override init() {
self.latitude = 0.0
self.longitud = 0.0
self.locationManager.requestWhenInUseAuthorization()
super.init()
if CLLocationManager.locationServicesEnabled() {
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
self.locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
internal func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = (manager.location?.coordinate)!
//print("locations = \(locValue.latitude) \(locValue.longitude)")
self.latitude = locValue.latitude
self.longitud = locValue.longitude
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
func getLatitude()->NSNumber{
return self.latitude as NSNumber
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
func getLongitude()->NSNumber{
return self.longitud as NSNumber
}
}
didUpdateLocations not getting called swift 3
Make sure you assign the delegate:
self.locationManager.delegate = self
CLLocationManager requestLocation not calling didUpdateLocations
Without seeing your full main window code, I bet that the problem is with the scope and lifecycle of your controller:
override func viewDidLoad() {
let weather = DarkSkyWeatherController()
weather.weatherGetterDelegate = self
weather.getLocation()
// Function exits. The weather constant dies off.
// This is why you don't get callbacks.
}
Do the following, instead.
let weather = DarkSkyWeatherController()
override func viewDidLoad() {
weather.weatherGetterDelegate = self
weather.getLocation()
// Function exits, but the weather constant lives on as a field of your main ViewController. You'll get your callbacks now.
}
didUpdateLocations not being called when app is in background
I have integrated NSLocationAlwaysUsageDescription in info.plist. Background modes have been enabled and Location Updates option has been selected
Good, but in iOS 9 there's a further requirement: you have to set the location manager's allowsBackgroundLocationUpdates
to YES. You are not doing that, so you're not going to get background location updates.
Related Topics
The Sandbox Is Not in Sync with the Podfile.Lock-Ios
Ios13 Share Sheet: How to Set Preview Thumbnail When Sharing Uiimage
Get Country Code from Country Name in iOS
Enable/Disable Apple Push Notification from iPhone App
Unnotificationattachment with Uiimage or Remote Url
How to Send Email Using Mfmailcomposeviewcontroller in Simulator
Leak from Nsurl and Avaudioplayer Using Arc
How to Do Weak Linking in Swift
How to Build Boost-Libraries for Iphone
Having App Restart Itself When It Detects Change to Privacy Settings
Main.Async VS Main.Sync() VS Global().Async in Swift3 Gcd
Make Uicollectionview Zoomable
How to Get Notified in Contact Changed Event in iOS
Find Uialertview Without Having Reference to It iOS 7