2

i am using swift4.2 and Xcode 10 and i am trying to make iOS app uses location service and it gives me exception:
Fatal error: Unexpectedly found nil while unwrapping an Optional value

so i tried to check if location returns nil so i copy my code and print location and it returns null , i simulated location in Xcode from product>scheme>edit scheme>default location and checked location in debug area and it simulated to location i choose any one know the problem?

import CoreLocation

class LocationVC: UIViewController,CLLocationManagerDelegate {

    let locationManager = CLLocationManager()
    var currentlocation:CLLocation!

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startMonitoringSignificantLocationChanges()
   }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        authorizelocationstates()
    }

    func authorizelocationstates(){
        if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
            currentlocation = locationManager.location
            print(currentlocation)
        }
        else{
            locationManager.requestWhenInUseAuthorization()
            authorizelocationstates()
        }
        }
Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
Khaled Bohout
  • 41
  • 1
  • 9
  • make sure to add it in plist – Mohmmad S Jan 27 '19 at 15:26
  • You have to implement the appropriate delegate methods for example [locationManager(_:didUpdateLocations:)](https://developer.apple.com/documentation/corelocation/cllocationmanagerdelegate/1423615-locationmanager) – vadian Jan 27 '19 at 15:26
  • Reference https://stackoverflow.com/questions/25296691/get-users-current-location-coordinates – nishith Singh Jan 27 '19 at 15:37
  • Your `authorizelocationstates` is likely to go into an infinite loop if the user does anything other than authorize location use. Please find some good tutorials on using Core Location. – rmaddy Jan 27 '19 at 17:17

1 Answers1

5

I have run your code and I just added missing key Privacy - Location When In Use Usage Description in info.plist file.

And I have made some changes in your code and get nil value because method getting called multiple time and when user give permission and method again called and going to print location detail but the fact is stilllocationManager variable has not user location data yet.

get location details in locationManager when delegate didUpdateLocations called

I have done some changes in your code:

import UIKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
    var locationManager = CLLocationManager()
    var currentlocation:CLLocation!



    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
     //   authorizelocationstates()
    }

    func authorizelocationstates(){
        if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
            currentlocation = locationManager.location
            print(currentlocation)
        }
        else{
            // Note : This function is overlap permission
            //  locationManager.requestWhenInUseAuthorization()
           //  authorizelocationstates()
        }
    }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        locationManager = manager
        // Only called when variable have location data
        authorizelocationstates()
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        // Get Location Permission one time only
        locationManager.requestWhenInUseAuthorization()
        // Need to update location and get location data in locationManager object with delegate
        locationManager.startUpdatingLocation()
        locationManager.startMonitoringSignificantLocationChanges()

    }

}

enter image description here

Hope it will help you.

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30