This is a app for weather in which i used weatherkit.I have added "privacy - Location when in Usage Description in info of project and also enabled location in features of simulator to Apple.After doing all this ,still the permission message for location is not appearing in app in simulator.Following is my code:
import CoreLocation
import UIKit
//final means this class will not be subclassed
final class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
//to access weather data
let service = WeatherService()
override func viewDidLoad() {
super.viewDidLoad()
setupView()
getUserLocation()
}
func getUserLocation(){
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func getWeather(location : CLLocation){
//asynchoronus task
Task {
do {
//if it throws error
//await tells to wait until function gives results
let result = try await service.weather(for: location)
print("Current : "+String(describing: result.currentWeather))
print("Daily : "+String(describing: result.dailyForecast))
print("Hourly : "+String(describing: result.hourlyForecast))
} catch {
//it will be excecuted
print(error.localizedDescription)
}
}
}
func setupView(){
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first else {
return
}
locationManager.stopUpdatingLocation()
getWeather(location: location)
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .denied:
print("location permission denied")
case .authorizedWhenInUse, .authorizedAlways :
manager.startUpdatingLocation()
print("Location Permission Granted")
case .notDetermined :
print("location Permission not determined")
default:
break
}
}
}