0

I'm trying to figure out how to get someones current location using SWIFT for a iOS app in XCODE.

I was following this tutorial here, but for some reason my code isn't working. I think it's because I am using a more updated Xcode versioning. For some reason my delegate has different parameters and I'm not sure why, but when I build my program it isn't even asking me if I want to allow location services at all.

Here is my code

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

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

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

    // GPS STUFF
    // UPDATE LOCATION
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        CLGeocoder().reverseGeocodeLocation(manager.location!) { (placemarks, ErrorType) -> Void in
            if(ErrorType != nil)
            {
                print("Error: " + ErrorType!.localizedDescription);
                return;
            }

            if(placemarks?.count > 0)
            {
                let pm = placemarks![0] ;
                self.displayLocationInfo(pm);
            }
        }
    }

    // STOP UPDATING LOCATION
    func displayLocationInfo(placemark: CLPlacemark)
    {
        self.locationManager.stopUpdatingLocation();
        print(placemark.locality);
        print(placemark.postalCode);
        print(placemark.administrativeArea);
        print(placemark.country);
    }

Also, in info.plist I also added NSLocationWhenInUseUsageDescription.

Any advice would be great

Termininja
  • 6,620
  • 12
  • 48
  • 49

1 Answers1

0

In your Info.plist, add this:

enter image description here

or you can add like this in your Info.plist:

<key>NSLocationAlwaysUsageDescription</key>
    <string>To get current location</string>

This will ask you to allow location services.

Hope this help you.

Twitter khuong291
  • 11,328
  • 15
  • 80
  • 116