0

App runs in iPhone but when i am trying to run in simulator its shows error as given in image

please help me & thank you in advanced

// THIS IS MY CODE:

    locationManager.delegate = self
    var locManager = CLLocationManager()
    locManager.requestWhenInUseAuthorization()


    if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse ||
        CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized)
    {
        let latitude1 = locManager.location!.coordinate.latitude.description
        latitude = latitude1

        let longitude1 = locManager.location!.coordinate.longitude.description
        longnitude = longitude1
        print(latitude)
        print(longnitude)
    } else {
        latitude = ""
        longnitude = ""

    }

enter image description here

Siddharth Shah
  • 382
  • 4
  • 22

2 Answers2

1

Try this code: Tested in Xcode simulator(Swift 3)

Update your plist:

Privacy - Location When In Use Usage Description == Some Value

 import UIKit
 import MapKit
 import CoreLocation

  class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

  @IBOutlet weak var mapView: MKMapView!

  let locationManager = CLLocationManager()

  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()
    self.mapView.showsUserLocation = true

}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    let location = locations.last

    let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)

    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2))

    self.mapView.setRegion(region, animated: true)

    self.locationManager.stopUpdatingLocation()

    print(location)
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
    print("Errors: " + error.localizedDescription)
}
}

Output from the code:

enter image description here

Joe
  • 8,868
  • 8
  • 37
  • 59
0

You'll have to import the framework as import CoreLocation

Arif Vadkey
  • 101
  • 3