import UIKit
import CoreLocation
class WeatherViewController: UIViewController {
@IBOutlet weak var conditionImageView: UIImageView!
@IBOutlet weak var searchTextField: UITextField!
@IBOutlet weak var TempratureLabel: UILabel!
@IBOutlet weak var CityLabel: UILabel!
var locationManager = CLLocationManager()
var weathermanager = WeatherManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.requestLocation()
// Do any additional setup after loading the view.
weathermanager.delegate = self
searchTextField.delegate = self
// print("Hii this is console. ")
}
}
// MARK: – UITextFieldDelagate
extension WeatherViewController:UITextFieldDelegate{
@IBAction func searchButtonPressed(_ sender: UIButton) {
searchTextField.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
searchTextField.endEditing(true)
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
if textField.text != "" {
return true
}else{
textField.placeholder = "Write Valid Location"
return false
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
if let cityname = textField.text {
weathermanager.fetchWeather(cityname: cityname)
// CityLabel.text = cityname
}else{
print("invalid city name typed")
}
searchTextField.text = ""
}
}
// MARK: – WeatherManagerDelegate
extension WeatherViewController:WeatherManagerDelegate {
func didupdateWeather(weather:WeatherModel){
//print(weather.temprature)
DispatchQueue.main.async {
self.TempratureLabel.text = weather.tempratureString
self.CityLabel.text = weather.cityname
self.conditionImageView.image = UIImage(systemName: weather.conditionName)
}
}
}
extension WeatherViewController:CLLocationManagerDelegate{
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
let lat = location.coordinate.latitude
let lon = location.coordinate.longitude
print(lat)
print(lon)
weathermanager.fetchWeather( latitute:lat, longitute:lon)
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: any Error) {
print(error)
}
}
I want solution for this getting this error again and again Error Domain=kCLErrorDomain Code=1 (null)”
what’s wrong
Kundan ios is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.