J86
J86

Reputation: 15237

CLLocationManager didUpdateLocations not being called

I am in the process of learning iOS 8 app development with Swift. I have followed a tutorial on Treehouse that walks you through building a weather app in Swift and iOS 8.

As an improvement to the app, the author/tutor suggests using CLLocationManager to get the location of the device to feed into the weather API instead of the hard coded latitude and longitude values.

So having read various tutorial online, I have gone ahead and attempted to implement this suggested improvement.

I have placed the code responsible for getting the location coordinates inside the AppDelegate.swift file.

AppDelegate.swift Code

import UIKit
import CoreLocation

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {

    var window: UIWindow?
    var locationManager: CLLocationManager!
    var errorOccured: Bool = false
    var foundLocation: Bool = false
    var locationStatus: NSString = "Not Started"
    var location: CLLocationCoordinate2D?
    var locationName: String?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        application.setStatusBarHidden(true, withAnimation: .None)
        initializeLocationManager()
        return true
    }

    func initializeLocationManager() {
        self.locationManager = CLLocationManager()
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
        self.locationManager.requestAlwaysAuthorization()
        self.locationManager.startUpdatingLocation()
    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        println("didUpdateLocations running")
        if (foundLocation == false) {
            self.locationManager.stopUpdatingLocation()
            foundLocation = true
            var locationArray = locations as NSArray
            var locationObj = locationArray.lastObject as CLLocation
            var geoCoder = CLGeocoder()
            geoCoder.reverseGeocodeLocation(locationObj, completionHandler: { (placemarks, error) -> Void in
                var p = placemarks as NSArray
                var placemark: CLPlacemark? = p.lastObject as? CLPlacemark
                self.locationName = placemark?.name
            })
            self.location = locationObj.coordinate
        }
    }

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
        locationManager.stopUpdatingLocation()
        if ((error) != nil) {
            if (errorOccured == false) {
                errorOccured = true
                print(error)
            }
        }
    }

    // authorization status
    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)")
            }
    }

}

And then in my ViewController.swift file I want to obtain the location coordinates. Here is the code:

ViewController.swift Code

func getCurrentWeatherData() -> Void {
    let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
    var forecastURL: NSURL
    var locName = "London"

    let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    appDelegate.foundLocation = false

    if let loc = appDelegate.location {
        println("Got Location!") // for debug purposes
        var currentLat = loc.latitude
        var currentLng = loc.longitude
        forecastURL = NSURL(string: "\(currentLat),\(currentLng)", relativeToURL: baseURL)
        locName = appDelegate.locationName!
    } else {
        println("No Location :(") // for debug purposes
        var currentLat = "51.513445"
        var currentLng = "-0.157828"
        forecastURL = NSURL(string: "\(currentLat),\(currentLng)", relativeToURL: baseURL)
    }

    let sharedSession = NSURLSession.sharedSession()

    let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
        var urlContents = NSString.stringWithContentsOfURL(location, encoding: NSUTF8StringEncoding, error: nil)
        if (error == nil) {
            let dataObject = NSData(contentsOfURL: location)
            let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject, options: nil, error: nil) as NSDictionary
            let currentWeather = Current(weatherDictionary: weatherDictionary)
            dispatch_async(dispatch_get_main_queue(), {
                () -> Void in
                self.locationNameLabel.text = "\(locName)"
                self.temperatureLabel.text = "\(currentWeather.temperature)"
                self.iconView.image = currentWeather.icon!
                self.currentTimeLabel.text = "At \(currentWeather.currentTime!) it is"
                self.humidityLabel.text = "\(currentWeather.humidity)"
                self.percipitationLabel.text = "\(currentWeather.percipProbability)"
                self.summaryLabel.text = "\(currentWeather.summary)"
                // Stop refresh animation
                self.refreshActivityIndicator.stopAnimating()
                self.refreshActivityIndicator.hidden = true
                self.refreshButton.hidden = false
            })
        } else {
            let networkIssueController = UIAlertController(title: "Error", message: "Unable to load data. Connectivity error!", preferredStyle: .Alert)
            let okButton = UIAlertAction(title: "OK", style: .Default, handler: nil)
            networkIssueController.addAction(okButton)
            let cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
            networkIssueController.addAction(cancelButton)
            self.presentViewController(networkIssueController, animated: true, completion: nil)

            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.refreshActivityIndicator.stopAnimating()
                self.refreshActivityIndicator.hidden = true
                self.refreshButton.hidden = false
            })
        }
    })

    downloadTask.resume()
}

The above is not working. My didUpdateLocations delegate never gets called. And in the debug console/output I always get No Location :( printed out, suggesting a failure in getting the location, more specifically suggesting that the location property on my AppDelegate is nil.

Things I have done to remedy this:

  1. In the info.plist I have added the two keys NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription
  2. Ensured that I am connected via WiFi and not Ethernet

And countless other code tweaks, and still nothing.

Upvotes: 2

Views: 13309

Answers (2)

LONGI
LONGI

Reputation: 11433

Just for the record: I first put the two keys (NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription) into the test-plist instead of the application-plist..Took me some time to realize.....

Upvotes: 3

Rob
Rob

Reputation: 437372

A couple of observations:

  1. As you point out, if you're going to call requestAlwaysAuthorization, then you must set NSLocationAlwaysUsageDescription. If you called requestWhenInUseAuthorization, you'd need NSLocationWhenInUseUsageDescription. (The fact that you see the confirmation dialog means that you've done this correctly. I assume you are seeing whatever description you supplied in the confirmation alert.)

  2. On your simulator, you may not see location updates like on a device. Test this on an actual device.

    When I used your code, I see didUpdateLocations when I called this from a device, but not from the simulator.

  3. Once you solve the issue of not seeing didUpdateLocations being called, there is another issue:

    You are posting a notification when the authorization status changes, but not when a location is received asynchronously (i.e. later). Frankly, the latter is the more critical event from the view controller's perspective, so I would have thought that (a) you should post a notification when the location is received; and (b) the view controller should observe this notification. Right now, even if you succeed in getting didUpdateLocations to be called, the view controller won't be notified of such.

    Also, your didUpdateLocations is initiating yet another asynchronous process, the geocode of the coordinate. If your view controller needs that, too, you should post a notification inside the completion block of the geocoder.

    Frankly, you haven't even shown us the view controller code that adds an observer for whatever notifications that this CLLocationManagerDelegate code will invoke, but I assume you have done that.

Upvotes: 7

Related Questions