Location Services will not run in new ViewController class

newbie Swift guy here - I had locations services running perfectly in a viewcontroller class, however I realised it logically sits in earlier view controller class and moved it here, however since then the location services are totally skipped over, only seque is run. I have checked the obvious (viewcontroller is listed in file inspector). Any help greatly appreciated,

import UIKit
import CoreLocation

var userLat = Double()
var userLong = Double()

class FirstViewController: UIViewController, CLLocationManagerDelegate {

    //instantiate location manager
    var locationManager = CLLocationManager()

    //get location data
    func determineMyCurrentLocation() {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()

        //start updating location
        if CLLocationManager.locationServicesEnabled() {
            locationManager.startUpdatingLocation()
        }
    }

    //get coordinates
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let userLocation:CLLocation = locations[0] as CLLocation

        //check to see if user has allowed app to see location and use those lats if is the case
        if CLLocationManager.locationServicesEnabled() {
        userLat = userLocation.coordinate.latitude
        userLong = userLocation.coordinate.longitude
        }

        //check if you got any data?
        print("user latitude = \(userLocation.coordinate.latitude)")
        print("user longitude = \(userLocation.coordinate.longitude)")

        //stop updating location
        manager.stopUpdatingLocation()

    }


    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
    {
        print("Error \(error)")
    }


    //perform Seques
    @IBAction func CheckLocation(_ sender: Any) {
        //cecking if user location is enableded?
        if CLLocationManager.locationServicesEnabled(){

            performSegue(withIdentifier: "two", sender: self)

        }

        else{
            performSegue(withIdentifier: "three", sender: self)
        }

    }

    @IBAction func Locatiohn(_ sender: Any) {

        performSegue(withIdentifier: "three", sender: self)
    }




    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "sun.jpg")!)
        // Do any additional setup after loading the view.

    }


    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated   )


    }



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



    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    //override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.






}

1 answer

  • answered 2018-10-22 21:49 Frank Ferdinand McGovern

    I fixed this by adding the removing 'func' from following code and placing it in viewDidLoad .... \

    func determineMyCurrentLocation() {
            locationManager = CLLocationManager()
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.requestAlwaysAuthorization()
    
            //start updating location
            if CLLocationManager.locationServicesEnabled() {
                locationManager.startUpdatingLocation()
            }