I am trying to get user location only one time when app is opening first time. (Other openings I do not want to know it)
I do not want allow from user.
I am using below code but it did not tell me location and it gives me error like: The operation couldn’t be completed. (kCLErrorDomain error 0.)
PS: I am not using simulator I am trying with real device
Codes:
import CoreLocation
import UIKit
class ViewController: UIViewController, CLLocationManagerDelegate {
let manager = CLLocationManager()
override func viewDidLoad() {
manager.delegate = self
manager.requestLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
print("Found user's location: \(location)")
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Failed to find user's location: \(error.localizedDescription)")
}
}