Location Manager not updating location in swift. AlertView disappears - ios

I want to get location inside a custom delegate in swift. Note that this worked perfectly 2 hours ago. The major problem is that the Location authorization alertView disappears by its own before I get to Allow it. So i tried to go within settings and allow it but it does not work. Why is the alertView disappearing by it self and why even though I allowed it through the settings I still cannot get an update? I added the correct key in the plist and also added the delegate and CoreLocation framework in the file. Also note that the didFail is not called at any point. Any advise would be appreciated
func getLocation(){
println("called")
let locationManager:CLLocationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
let status = CLLocationManager.authorizationStatus()
println(status.rawValue)
if(status != CLAuthorizationStatus.Authorized) {
locationManager.requestWhenInUseAuthorization()
println("called2")
}else{
locationManager.startUpdatingLocation()
println("allowed and updating")
}
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
println("updating")
var locationArray = locations as NSArray
var locationObj = locationArray.lastObject as CLLocation
println(locationObj)
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println(error)
}
func locationManager(manager: CLLocationManager!,
didChangeAuthorizationStatus status: CLAuthorizationStatus) {
var shouldIAllow = false
println(status)
switch status {
case CLAuthorizationStatus.Restricted:
println("Restricted Access to location")
case CLAuthorizationStatus.Denied:
println("User denied access to location")
case CLAuthorizationStatus.NotDetermined:
println("Status not determined")
default:
println("Allowed to location Access")
shouldIAllow = true
}
if (shouldIAllow == true) {
manager.startUpdatingLocation()
} else {
println("Denied access: \(status)")
}
}

Create a property from locationManager , because this way it is destroyed after you run your method. And don't forget to setup its delegate for example in viewDidLoad.

Related

swift - didUpdateLocations Not Being Called

I'm working on an app that requires getting the user's current coordinates. I was planning on doing this through CLLocationManager's didUpdateLocations method. For some reason, didUpdateLocations is not being executed. However, it appears that locationManager.startUpdatingLocation() is being called successfully. None of the other possible solutions I've seen on this site have worked for me. I already added NSLocationAlwaysUsage to info.plist. Here is the entirety of my code:
import UIKit
import MapKit
import CoreLocation
var region: MKCoordinateRegion!
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
#IBOutlet weak var map: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()
switch CLLocationManager.authorizationStatus() {
case .authorizedWhenInUse, .authorizedAlways:
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
print("Updating location now")
}
case .notDetermined:
locationManager.requestAlwaysAuthorization()
case .restricted, .denied:
print("User must enable access in settings")
break
}
if (region == nil){
}
else {
map.setRegion(region!, animated: true)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("Got location")
let userLocation:CLLocation = locations[0]
let lat:CLLocationDegrees = userLocation.coordinate.latitude
let long:CLLocationDegrees = userLocation.coordinate.longitude
let currentPos:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, long)
didUpdateRegion(position: currentPos)
print(lat)
print(long)
}
func didUpdateRegion(position: CLLocationCoordinate2D) {
let span = MKCoordinateSpanMake(0.075, 0.075)
region = MKCoordinateRegion(center: position, span: span)
}
func locationManager(_manager: CLLocationManager, didFailWithError error: Error) {
print(error.localizedDescription)
}
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .notDetermined:
// If status has not yet been determied, ask for authorization
manager.requestWhenInUseAuthorization()
break
case .authorizedWhenInUse:
// If authorized when in use
manager.startUpdatingLocation()
break
case .authorizedAlways:
// If always authorized
manager.startUpdatingLocation()
break
case .restricted:
print("User must activate location services in settings")
break
case .denied:
print("User must activate location services in settings")
break
default:
break
}
}
When I run this code on both the simulator and an actual device, I get the notification to allow location tracking. After accepting that, the console displays "Updating location now," but never gets to printing "Got location." Thank you for any light you can shed on this issue, I'm new to app development in general.
EDIT: I added in the entirety of my code instead of just the parts I thought were relevant. Basically, I'm trying to get the region shown on the map to follow the user. I attempt to do this by updating the variable "region" every time the didUpdateLocations function fires.
Am I getting it right and you only added one key - NSLocationAlwaysUsage?
Try to add both keys to the Info.plist:
Privacy - Location When In Use Usage Description
Privacy - Location Always Usage Description
Also, what happens if you implement this method of protocol?
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("\(error.localizedDescription)")
}
Does it print anything? Sorry, I was going to leave a comment, but I don't have enough reputation.

Issues getting device/user location with iOS App, Swift

Setup:
Xcode 7.3 (7D175)
Swift 2
Device is an iPad with iOS 9.3.1
I have a Swift class called LocationUtility, I am using it like so in a ViewController:
let locationUtil = LocationUtility()
locationUtil.initLocationManager()
The initLocationManager() function sets the CLLocationManager.delegate to my LocationUtility class:
func initLocationManager() {
seenError = false
locationFixAchieved = false
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
}
}
The following delegate func is never being called:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
I have NSLocationAlwaysUsageDescription string set in my Info.plist
On my iPad, Privacy -> Location Services is Enabled.
My LocationUtility class is something a put together with, mostly code from other stackoverflow questions and answers about location functionality in Swift and iOS 8 and beyond. From my perspective I have all the right settings in my APP and on my device to be receiving location information.
Here is my complete LocationUtility class source code:
import UIKit
import Foundation
import CoreLocation
class LocationUtility: UIViewController, CLLocationManagerDelegate {
var locationStatus : NSString = "Not Started"
var locationManager: CLLocationManager!
var seenError : Bool = false
var locationFixAchieved : Bool = false
var currentLocation:CLLocation? = nil
func initLocationManager() {
seenError = false
locationFixAchieved = false
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
//locationManager.delegate = self
//locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("LocationUtility -> didFailWithError()")
locationManager.stopUpdatingLocation()
if (seenError == false) {
seenError = true
print(error)
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("LocationUtility -> didUpdateLocations()")
if (locationFixAchieved == false) {
locationFixAchieved = true
//var locationArray = locations as NSArray
currentLocation = locations.last! as CLLocation
let coord = currentLocation!.coordinate
print("user current location")
print(coord.latitude)
print(coord.longitude)
}
}
// authorization status
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
print("LocationUtility -> didChangeAuthorizationStatus()!")
var shouldIAllow = false
switch status {
case CLAuthorizationStatus.Restricted:
locationStatus = "Restricted Access to location"
case CLAuthorizationStatus.Denied:
locationStatus = "User denied access to location"
case CLAuthorizationStatus.NotDetermined:
locationStatus = "Status not determined"
default:
locationStatus = "Allowed to location Access"
shouldIAllow = true
}
if (shouldIAllow == true) {
NSLog("Location to Allowed")
// Start location services
locationManager.startUpdatingLocation()
} else {
NSLog("Denied access: \(locationStatus)")
}
}
}
The symptoms you describe strongly suggest that your LocationUtility instance is getting deallocated, which deallocates the CLLocationManager and stops the entire process. It's not clear where you're instantiating LocationUtility but you need to make sure it's some place where the instance will remain "live" in memory while CLLocationManager does its things. If the view controller is deallocated, for example, then its instance vars will be deallocated, which looks like it probably includes your location manager.
Your question initially asked if the location manager had to be in the app delegate. It doesn't, of course, but it does have to be someplace that can prevent it from being deallocated while location updates are in progress.
If you're unsure when the LocationManager is getting deallocated, try implementing deinit on it and setting a breakpoint on that method.

Why isn't locationManager didUpdateLocation working?

With CoreLocation.Foundation added to the BuildPhase and imported at the top of the file, I can get location information if I put the following into a view controller with a button:
#IBAction func locationButton(sender: AnyObject) {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
It goes on to locationManager didUpdateLocations with a CLGeocoder().reverseGeocodeLocation completionHandler that displays the location info in another function - this works.
BUT, it doesn't work when I try to transfer this same code in my data model. I set up the model with the following:
import CoreLocation
class Record: NSObject, CLLocationManagerDelegate
{
let locationManager = CLLocationManager()
Because there's no button, I've put the locationManager code into:
override init()
{
iD = NSUUID().UUIDString
super.init()
if (CLLocationManager.locationServicesEnabled())
{
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
switch CLLocationManager.authorizationStatus() {
case .AuthorizedWhenInUse, .AuthorizedAlways:
locationManager.startUpdatingLocation()
case .NotDetermined:
locationManager.requestWhenInUseAuthorization() // or request always if you need it
case .Restricted, .Denied:
print("tell users that they need to enable access in settings")
default:
break
}
print("Location services available")
if CLLocationManager.authorizationStatus() == .NotDetermined
{
print("Still Not Determined")
}
} else { print("Location services not available") }
}
I get 'Location services available'. But the following code never prints anything to console, nor does it call the function toSetLocationStamped.
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
{
print("started location man")
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: //pass the location co-ordinates
{
(placemarks, error) -> Void in
if (error != nil)
{
println("Reverse geocoder failed with error" + error.localizedDescription)
return
}
if placemarks.count > 0 //process the location array (placemarks)
{
let pm = placemarks[0] as! CLPlacemark
self.toSetLocationStamped(pm)
print("got here")
} else
{
println("Problem receiving data from geocoder")
}
})
}
If I put a deinit the class Record, with a simple print log, there is no output.
deinit
{
print("deinit")
}
I'm initializing a dummyRecord: Record from a required init in MasterViewController class:
class MasterViewController: UITableViewController
{
var records = [Record]()
var subjectDescription: String?
// weak var delegate: MonsterSelectionDelegate? // property for object conforming to MSDelegate
required init(coder aDecoder: NSCoder) // // coder because class is loaded from Storyboard
{
super.init(coder: aDecoder)
var dummyRecord1 = Record()
dummyRecord1.details = "All was very good and strong with a little bit of lemon on the side of the hill."
dummyRecord1.dateTimeEntered = NSDate(dateString: "2015-07-22")
dummyRecord1.subject = "Clouds"
dummyRecord1.locationEntered = "Drittelsgasse 1, 69493 Großsachsen, Germany."
dummyRecord1.photos.append(UIImage(named: "zombies.jpg")!)
records.append(dummyRecord1)
}
After you call
self.locationManager.requestWhenInUseAuthorization()
you cannot immediately start updating location. That call is asynchronous.
Here is how you should properly do it:
1) Check the authorisation status:
switch CLLocationManager.authorizationStatus() {
case .AuthorizedWhenInUse, .AuthorisedAlways:
locationManager.startUpdatingLocation()
case .NotDetermined:
locationManager.requestWhenInUseAuthorization() // or request always if you need it
case .Restricted, .Denied:
// tell users that they need to enable access in settings
default:
break
}
2) If you have previously authorised your app this should update location. However, if you didn't a popup will appear. In order to respond to the change in the authorisation status you need to add another function:
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if (status == .AuthorizedAlways) || (status == .AuthorizedWhenInUse) {
locationManager.startUpdatingLocation()
}
}

MapView doesn't ask for location permission

I'm trying to build a mapView in Swift. It already worked but after I changed something I can't remember, I'm now getting the following error:
Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
The NSLocationAlwaysUsageDescription is in my .plist file.
Here is the code:
import UIKit
import MapKit
class AwesomeMap : UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
var map: MKMapView?
var manager: CLLocationManager?
func setup() {
manager = CLLocationManager()
manager!.delegate = self
map!.delegate = self // map is being set from another controller
manager!.requestAlwaysAuthorization()
manager!.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
println("permisison did change")
if(status == CLAuthorizationStatus.AuthorizedWhenInUse || status == CLAuthorizationStatus.Authorized) {
map!.showsUserLocation = true
}
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
for location in (locations as Array) {
var loc = (location as CLLocation)
println(loc.coordinate.latitude)
let region = MKCoordinateRegion(center: loc.coordinate, span: MKCoordinateSpanMake(0.05, 0.05))
map!.setRegion(region, animated: true)
}
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("fail")
}
}
Old code before implementing a few suggestions:
import UIKit
import MapKit
class AwesomeMap : UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
var manager = CLLocationManager()
var map: MKMapView?
func setup(mapView: MKMapView) { // passing the IBOutlet from another controller
// the change I made was around the following six lines I think...
map = mapView
map!.delegate = self
manager.delegate = self
manager.requestAlwaysAuthorization()
map!.showsUserLocation = true
manager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
for location in (locations as Array) {
var loc = (location as CLLocation)
println(loc.coordinate.latitude)
let region = MKCoordinateRegion(center: loc.coordinate, span: MKCoordinateSpanMake(0.05, 0.05))
map!.setRegion(region, animated: true)
}
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("fail") // nothing happens here
}
}
You're only allowed to call
map!.showsUserLocation = true
after your Location Manager has got the Permission to use the user Location.
Like in (got no swift version of this, but you'll probably get the point)
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
if(status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways)
{
self.mapView.showsUserLocation = YES;
}
}
Go to Simulator settings
after that enter Location Services and turn on . if you need location blue dote in MapView enter <> and set << While Using the App >>
Do not call showsUserLocation when authorization status is undetermined.
func setup(mapView: MKMapView) { // passing the IBOutlet from another controller
map = mapView
map!.delegate = self
manager.delegate = self
manager.requestAlwaysAuthorization() // Does not if status is determined.
self.locationManager(manager: manager, status: CLLocationManager.authorizationStatus())
}
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .Authorized {
map!.showsUserLocation = true
manager.startUpdatingLocation()
}
}
I have this working code in my app (reduced):
if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized {
manager.startUpdatingLocation()
}
additionaly, you can react on any user's authorization status changes:
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .Authorized {
manager.startUpdatingLocation()
}
}

Implement CLLocationManagerDelegate methods in Swift

I've been trying to get this to work for awhile now, and I've come here to ask- how do I go about with using the CLLocationManagerDelegate methods in Swift? I've put this at the top of my class:
var locationManager = CLLocationManager()
I've put the following into my viewDidLoad method:
locationManager.delegate = self
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
And I've tried using these delegate methods with no avail:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: AnyObject[]!) {
locationReceived = true
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
locationReceived = false
}
I've also tried using #optional in front of the functions, but Xcode then throws a compiler error. Any ideas?
You need to add the NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key to your plist if you haven't already, they are now mandatory,
iOS8+ requires one of these two strings to be set to use locations. Which one you use depends on how you intend ask for the location.
Use NSLocationAlwaysUsageDescription for apps that want to use the device's location even when the app is not open and being used.
Use NSLocationWhenInUseUsageDescription for apps that want to use the device's location only when the app is open and in use.
Note: When you add the strings, before you build and run, delete the app off your device and let it do a fresh install. It seems that if the app was authorized to use locations before you upgraded to iOS8 it doesn’t ask for your permission again and doesn’t see that you set those strings. Doing a delete and clean install solves this.
Setting either of the strings prompts a pop up on install/first use along the lines of: "Allow "ThisApp" to access your location even when you are not using the App"
Here's a Screenshot of the plist file.
First add this two line in plist file
1) NSLocationWhenInUseUsageDescription
2) NSLocationAlwaysUsageDescription
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate
var seenError : Bool = false
var locationFixAchieved : Bool = false
var locationStatus : NSString = "Not Started"
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
}
func initLocationManager() {
seenError = false
locationFixAchieved = false
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.locationServicesEnabled
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
locationManager.stopUpdatingLocation()
if (error) {
if (seenError == false) {
seenError = true
print(error)
}
}
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: AnyObject[]!) {
if (locationFixAchieved == false) {
locationFixAchieved = true
var locationArray = locations as NSArray
var locationObj = locationArray.lastObject as CLLocation
var coord = locationObj.coordinate
println(coord.latitude)
println(coord.longitude)
}
}
func locationManager(manager: CLLocationManager!,
didChangeAuthorizationStatus status: CLAuthorizationStatus) {
var shouldIAllow = false
switch status {
case CLAuthorizationStatus.Restricted:
locationStatus = "Restricted Access to location"
case CLAuthorizationStatus.Denied:
locationStatus = "User denied access to location"
case CLAuthorizationStatus.NotDetermined:
locationStatus = "Status not determined"
default:
locationStatus = "Allowed to location Access"
shouldIAllow = true
}
NSNotificationCenter.defaultCenter().postNotificationName("LabelHasbeenUpdated", object: nil)
if (shouldIAllow == true) {
NSLog("Location to Allowed")
// Start location services
locationManager.startUpdatingLocation()
} else {
NSLog("Denied access: \(locationStatus)")
}
}
To get User Current Location :-
Step 1: let locationManager = CLLocationManager() // make object of CLLocationManager class.
Step 2: In viewDidLoad instantiate the CLLocationManager class like,
// For use in background
self.locationManager.requestAlwaysAuthorization()
// For use in foreground
self.locationManager.requestWhenInUseAuthorization()
if (CLLocationManager.locationServicesEnabled())
{
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
Step 3: Now implement the delegate methods of CLLocationManager
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var locValue:CLLocationCoordinate2D = manager.location.coordinate
println("locations = \(locValue.latitude) \(locValue.longitude)")
}
Step 4:
Don't forget to add NSLocationAlwaysUsageDescription in the Info.plist as in iOS 8 it is mandatory to add this. This will ask permission to use user's location.
I had the same issue. didUpdateLocations - was not working. Run your app.
Go to the Settings page -> Privacy -> Location and turn off Location Services. didFailWithError will catch the error about absent Location Services. Then turn it on. Since that moment didUpdateLocations will catch locations.
This is a bit of an old thread, yet none of the above worked for me on Swift 2.2 xCode 7.3.1.
The problem is I was using:
func locationManager(manager: CLLocationManager!, didUpdateLocation locations: [AnyObject]!) {
print("Got location")
locationManager.stopUpdatingLocation()
}
And this never got called. When I changed to:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("Got location")
locationManager.stopUpdatingLocation()
}
It all worked out. Seems like the delegate is not called when using [AnyObject]

Resources