7

I'm trying to create a simple app that finds out the region someone is in, but I'm stuck because none of the CLLocationManagerDelegate methods are called when the app runs and finds locations.

In case it's relevant I'm also not seeing the dialog asking that I give the app permission to use location.

Here's what I have so far -

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var locationLabel : UILabel!

var locationManager = CLLocationManager()
let geocoder = CLGeocoder ()

override func viewDidLoad() {
  super.viewDidLoad()
  // Do any additional setup after loading the view, typically from a nib.

  locationManager.delegate = self
  locationManager.desiredAccuracy = kCLLocationAccuracyBest
  locationManager.startUpdatingLocation()
}

override func viewDidDisappear(animated: Bool) {
  locationManager.stopUpdatingLocation()
}

override func didReceiveMemoryWarning() {
  super.didReceiveMemoryWarning()
  // Dispose of any resources that can be recreated.
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations:  [AnyObject]!) {
  geocoder.reverseGeocodeLocation(locations.last as CLLocation, completionHandler: {(placemark, error) in
    if (error != nil) {
      println("Error")
    } else {
      let pm = placemark.first as CLPlacemark

      println(pm)
    }
  })
}

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    println("Epic Fail")
  } 
}

I've put in breakpoints so I know the code is never called. I have gone in and manually turned on location services for the app while it's been running too.

Mark Reid
  • 2,611
  • 3
  • 23
  • 45
  • maybe duplicate of http://stackoverflow.com/questions/24063798/cllocation-manager-in-swift-to-get-location-of-user – kabarga Oct 17 '14 at 22:37

4 Answers4

3

try calling

func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!)

instead of the delegate call you are making now... I had problems with:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations:  [AnyObject]!)

aswell. But using the above delegate call fixed my issue.

Edit (didn't read the question properly...)

You never ask for permission, which is why you don't get a popup. Call the following: locationManager.requestWhenInUseAuthorization()

This is quite an important step, and your app won't work if you haven't asked the user for authorization of your app

It is also important that you add NSLocationWhenInUseUsageDescription to your .plist file if running iOS8.

Screenshot:

enter image description here

mixel
  • 25,177
  • 13
  • 126
  • 165
Chris
  • 7,830
  • 6
  • 38
  • 72
  • Thanks for the answer. I gave both of the suggestions a try and I'm still seeing the same result of no methods being called and no request for authorisation. I will add that I'm running this on a device rather than the simulator. – Mark Reid Oct 17 '14 at 22:59
  • 2
    Ok @Mark Reid, which iOS version are you running? Remember you have to add the following key / value to your .plist file: `NSLocationWhenInUseUsageDescription` : "I need to use your location for this that and the other" – Chris Oct 18 '14 at 08:07
  • It is iOS 8 and that's the problem I was having. Thanks. – Mark Reid Oct 18 '14 at 20:26
  • I am facing the same problem. I have follow all steps that Chris said. Would you please tell me how you solve problem finally @Mark Reid – Alamin Oct 15 '15 at 00:52
0

The following answer question and answer gave me the solution I needed.

CLLocationManager authorization issue iOS 8

Ultimately if you're running on iOS then you must add the key

NSLocationAlwaysUsageDescription (when requesting location data to be always available)

OR

NSLocationWhenInUseUsageDescription (when requesting to use location data only when your app is running)

If you do not do this the location services access will be denied by default for your app and the user will not be prompted to allow it.

Community
  • 1
  • 1
Mark Reid
  • 2,611
  • 3
  • 23
  • 45
0
private let loctionManager = CLLocationManager() // Here

class YourViewController: ViewController {
    loctionManager.delegate = self
    loctionManager.requestWhenInUseAuthorization()
}

extension YourViewController: CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if (status == .authorizedWhenInUse) {
            // do something
        }
    }
}

.plist
Property List
Key: Privacy - Location When In Use Usage Description
Type: String
Value: Use to add a location to your rap.
OR
Source code
<key>NSLocationWhenInUseUsageDescription</key>
<string>Use to add a location to your rap.</string>
Giang
  • 3,553
  • 30
  • 28
0

If you tried all other answers and still having issues, just make sure CLLocationManager.init() and startUpdatingLocation are called on the same thread, preferably main thread.

I personally had a project and took me a while to figure out!

Amir.n3t
  • 2,859
  • 3
  • 21
  • 28