Simple Weather App with current geolocation feature.
- API & URLSession for Networking
- JSON Decoding
- Delegate Pattern implementation
- Extensions for refactoring ViewController
- CoreLocation & Location Data
FILE.2022-02-15.09.58.51.mp4
import UIKit
import CoreLocation
class WeatherViewController: UIViewController {
@IBOutlet weak var conditionImageView: UIImageView!
@IBOutlet weak var temperatureLabel: UILabel!
@IBOutlet weak var cityLabel: UILabel!
@IBOutlet weak var searchTextField: UITextField!
var weatherManager = WeatherManager()
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.requestLocation()
weatherManager.delegate = self
searchTextField.delegate = self
}
@IBAction func locationPressed(_ sender: UIButton) {
locationManager.requestLocation()
}
}
//MARK: - UITextFieldDelegate
extension WeatherViewController: UITextFieldDelegate {
@IBAction func searchpressed(_ sender: UIButton) {
searchTextField.endEditing(true)
print(searchTextField.text!)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
searchTextField.endEditing(true)
return true
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
if textField.text != "" {
return true
} else {
textField.placeholder = "Type something"
return false
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
if let city = searchTextField.text {
weatherManager.fetchWeather(cityName: city)
}
searchTextField.text = ""
}
}
//MARK: - WeatherManagerDelegate
extension WeatherViewController: WeatherManagerDelegate {
func didUpdateWeather(_ weatherManager: WeatherManager, _ weather: WeatherModel) {
DispatchQueue.main.async {
self.temperatureLabel.text = weather.temperatureString
self.conditionImageView.image = UIImage(systemName: weather.conditionName)
self.cityLabel.text = weather.cityName
}
}
func didFailWithError(error: Error) {
print(error)
}
}
//MARK: - CLLocationManagerDelegate
extension WeatherViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
locationManager.stopUpdatingLocation()
let lat = location.coordinate.latitude
let lon = location.coordinate.longitude
weatherManager.fetchWeather(latitude: lat, longitute: lon)
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
}
}