I have a problem because my app crash when i try to get position user and have permission in my .plist this my code.
import UIKit
import MapKit
import CoreLocation
class mapClass : UIViewController,MKMapViewDelegate,CLLocationManagerDelegate{
var location4 = CLLocation()
var lm = CLLocationManager ()
@IBOutlet var propMapa: MKMapView!
lm.delegate=self
lm.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
lm.distanceFilter = kCLDistanceFilterNone
lm.startUpdatingLocation()
println("\(lm.location)") <---- nil
location4 = lm.location <---- crash
}
Log: fatal error: unexpectedly found nil while unwrapping an Optional value
After calling lm.startUpdatingLocation()
the location manager calls didUpdateLocations()
whenever it detects a location change, so you need to implement CLLocationManager::didUpdateLocations
to retrieve the location:
import UIKit
import MapKit
import CoreLocation
class mapClass : UIViewController,MKMapViewDelegate,CLLocationManagerDelegate{
var location4 = CLLocation()
var lm = CLLocationManager ()
@IBOutlet var propMapa: MKMapView!
...
lm.delegate=self
lm.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
lm.distanceFilter = kCLDistanceFilterNone
lm.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let currentLocation = locations.last as CLLocation
println(currentLocation)
...
}