0

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")
        
    }
    
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
Senator
  • 73
  • 9

3 Answers3

1

You did mistake with CLLocationManager delegate conforming. UIViewController or other classes should implement delegates as protocols f.e. via extensions or just add CLLocationManagerDelegate after your class declaration. Try replace your file code with:

import UIKit
import MapKit
import CoreLocation
    
// MARK: - Location manager delegate

extension SignUpSettingsViewController: CLLocationManagerDelegate {

        func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
            switch status {
            case .authorizedAlways: break
            case .authorizedWhenInUse: break
            case .denied, .restricted: break /// location permission denied, or updated in Settings
            case .notDetermined: break /// location permission wasn't selected
            @unknown default: break
            }
        }
        
        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            guard let location = locations.last else { return }
            print(location)
      }    
}

// MARK: - SignUpSettingsViewController
    
final class SignUpSettingsViewController: UIViewController {
    
    @IBOutlet weak var locationOn: UISwitch!
        
    private let locationManager = CLLocationManager()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func locationOn(_ sender: UISwitch) {
        if (sender.isOn == true) {
            print("Location SLIDER TURNED ON")
            locationManager.requestWhenInUseAuthorization()
            locationManager.startUpdatingLocation()
            locationManager.delegate = self
        } else {
            locationManager.delegate = nil
            print ("Location SLIDER NOT ON")
        }
    }
}

inside this func you should handle you location permissions

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        switch status {
        case .authorizedAlways: break
        case .authorizedWhenInUse: break
        case .denied, .restricted: break /// location permission denied, or updated in Settings
        case .notDetermined: break /// location permission wasn't selected
        @unknown default: break
        }
    }
kefir
  • 21
  • 4
0

Take a look again. You defined func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) inside @IBAction func locationOn(_ sender: UISwitch) Move it inside class SignUpSettingsViewController

0

Just recheck your info.plist file for the location keys which needs to add.

Another step is You need to keep

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 

method out of the UISwitch action method scope. I run your same code by above changes, It Works for me.

For your reference, Please check below code:

var locationManager: CLLocationManager?
@IBOutlet weak var locationOn: UISwitch!

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")
            
        }
}
//MARK: Location service Delegate
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
            print("locations = \(locValue.latitude) \(locValue.longitude)")
        }
        
}

Good Luck :)

Dharman
  • 30,962
  • 25
  • 85
  • 135
Anjali jariwala
  • 410
  • 5
  • 15