0

I am trying to get user location in my iPhone app. It has specified in info.plist needed permission (use location even if app is not offline). But still I do not see that my delegate is triggered at least once meaning I do not get even current location. Here is the code:

import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {

var locationManager = CLLocationManager();
override func viewDidLoad() {


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

    locationManager.delegate = self

    if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.notDetermined) {
        self.locationManager.requestAlwaysAuthorization();
    }

    locationManager.distanceFilter = kCLDistanceFilterNone
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()
}

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

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLocation:CLLocation = locations[0]
    let long = userLocation.coordinate.longitude;
    let lat = userLocation.coordinate.latitude;


    print(lat)
 }

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

Could somebody point me out possible problems?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
seeker
  • 3,255
  • 7
  • 36
  • 68
  • It is ask for location permission or not? – Hitesh Surani Dec 23 '16 at 18:51
  • yes, it asked for permission once and I allowed. Every next program run I do not get this permission request since it is stored in settings. Thing is that delegate which should be triggered on location update is not actually called. I suspect this could be multithreading issue e.g. calling from main thread and on other thread that instance does not exists... checking now... – seeker Dec 23 '16 at 18:55
  • Possible duplicate of [How to get Location user whith CLLocationManager in swift?](http://stackoverflow.com/questions/24252645/how-to-get-location-user-whith-cllocationmanager-in-swift) – xoudini Dec 23 '16 at 19:10
  • Are you running on a device or on the simulator? – Paulw11 Dec 23 '16 at 21:00

1 Answers1

0

Try to remove locationManager.stopUpdatingLocation() in locationManager(manager: CLLocationManager!, didFailWithError error: NSError!). It should work.

Srj0x0
  • 458
  • 3
  • 12