I am trying to abstain coordinates from the device, but the app crash. Anyone know what might have happened? I suppose the error happened when it asked for permission but the request was denied, maybe creating an if statement it works, but I couldn't find any information about that.
output:fatal error: unexpectedly found nil while unwrapping an Optional value
import UIKit
import Foundation
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
println("\(locationManager.location.coordinate.latitude), \(locationManager.location.coordinate.longitude)")
self.locationManager.requestAlwaysAuthorization() // Ask for Authorisation from the User.
// For use in foreground
self.locationManager.requestWhenInUseAuthorization()
if (CLLocationManager.locationServicesEnabled())
{
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
self.locationManager.startUpdatingLocation()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var locValue:CLLocationCoordinate2D = manager.location.coordinate
var latitudeactual:Double = 0
var longitudeactual:Double = 0
latitudeactual = locValue.latitude
longitudeactual = locValue.longitude
locationManager.stopUpdatingLocation()
if latitudeactual != 0 || longitudeactual != 0 {
latitudeactual = locValue.latitude
longitudeactual = locValue.longitude
}
}
}