i've successfully setup location services in an iOS app i'm working on (user can choose to allow location services after moving a slider) but none of my code attempts to grab the user's current location (by long/lat) are working. Could use some help figuring out what may be wrong... thanks!
import UIKit
import MapKit
import CoreLocation
class SignUpSettingsViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var locationOn: UISwitch!
var locationManager: CLLocationManager?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func locationOn(_ sender: UISwitch) {
if (sender.isOn == true){
print("Location SLIDER TURNED ON")
locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.requestWhenInUseAuthorization()
locationManager?.startUpdatingLocation()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
print("locations = \(locValue.latitude) \(locValue.longitude)")
}
func locationManager2(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
print(location.coordinate.latitude)
print(location.coordinate.longitude)
}
}
}
else {
print ("Location SLIDER NOT ON")
}
}