UI won't update after getting json data from Openweathermap IOS 4 Xcode 10 - ios

my JSON have been printed to console with the correct value request. but label.text and city.text won't update UI in main controller
#IBOutlet weak var icon: UIImageView!
#IBOutlet weak var date: UILabel!
#IBOutlet weak var weatherType: UILabel!
#IBOutlet weak var degree: UILabel!
#IBOutlet weak var city: UILabel!
var currentWeather : CurrentWeather!
override func viewDidLoad() {
super.viewDidLoad()
currentWeather = CurrentWeather()
}
override func viewDidAppear(_ animated: Bool) {
super .viewDidAppear(animated)
locationAuthStatus()
}
func locationAuthStatus() {
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
currentLocaion = locationManager.location
//Location.sharedIntance.latitude = currentLocaion.coordinate.latitude
//Location.sharedIntance.longitude = currentLocaion.coordinate.longitude
print(currentLocaion)
currentWeather.downloadWeatherDetail {
downloadForecastData {
self.updateMainUI()
}
}
} else {
locationManager.requestWhenInUseAuthorization()
locationAuthStatus()
}
}
func updateMainUI() {
icon.image = UIImage(named: currentWeather.weatherType)
city.text = currentWeather.cityName
degree.text = "\(currentWeather.currentTemp)"
weatherType.text = currentWeather.weatherType
date.text = currentWeather.date
}
}
I am new to coding and 'm having a hard figuring out why isn't it showing since I confirmed that I am pulling data from Json to my console

These lines won't do what you expect.
locationManager.requestWhenInUseAuthorization()
locationAuthStatus()
Your call to requestWhenInUseAuthorization is asynchronous and should be handled by a CLLocationManagerDelegate which can be your view controller. You can do this like this:
class ViewController: UITableViewController, CLLocationManagerDelegate {
// rest of class
Add these functions to your class
func locationManager(_ manager: CLLocationManager, didUpdateLocations objects: [CLLocation]) {
let location = objects[objects.count-1]
let currentLocation = location
print(currentLocation)
currentWeather.downloadWeatherDetail {
downloadForecastData {
self.updateMainUI()
}
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
// handle error
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
locationManager.startUpdatingLocation()
}

Related

how to get user latitude and longitude in a label by clicking uibutton

I am trying to get user current location latitude and longitude by using reverse CLgeocoding.but,i think methods doesn't called. i am not getting any result in console at least.If any one helps me to do this,would be great.Thank in advance.
import UIKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
#IBOutlet weak var lbl2: UILabel!
#IBOutlet weak var lbl1: UILabel!
var str1: Double?
var str2: Double?
var locationManager:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
determineMyCurrentLocation()
}
func determineMyCurrentLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
//locationManager.startUpdatingHeading()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error) -> Void in
if (error != nil) {
print("ERROR:" + error!.localizedDescription)
return
}
if (placemarks?.count)! > 0 {
let pm = placemarks![0] as CLPlacemark
self.displayLocationInfo(placemark: pm)
} else {
print("Error with data")
}
})
}
func displayLocationInfo(placemark: CLPlacemark) {
// self.locationManager.stopUpdatingLocation()
print(placemark.locality)
print(placemark.postalCode)
print(placemark.administrativeArea)
print(placemark.country)
print(placemark.location)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
print("Error \(error)")
}
#IBAction func currentadd(_ sender: Any) {
}
First make sure you have added one of the keys in your Info.plist
NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription.
Second you need to call determineMyCurrentLocation method in your viewDidLoad or viewDidAappear. You can also call determineMyCurrentLocation from any button or event according to your need.
Edit
One more thing i would like to suggest is you should call manager.stopUpdatingLocation() in your didUpdateLocations if you don't want to use location service constantly.
Updated method
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let managerLocation = manager.location{
CLGeocoder().reverseGeocodeLocation(managerLocation) { (placemarks, error) in
if let locations = placemarks ,let location = locations.first {
self.displayLocationInfo(placemark: location)
}else if let error = error{
print("ERROR:" + error.localizedDescription)
}
}
}
}

MapView Not Showing Location

I'm currently developing an iOS app, and one of its functions include locating places of interest for nightlife. However, when my MapView loads, current user location is not displayed, even though location access has been granted.
Here is my code:
import UIKit
import MapKit
import CoreLocation
class BarMapVC: UIViewController {
#IBOutlet weak var searchBtn: UIBarButtonItem!
#IBOutlet weak var doneBtn: UIBarButtonItem!
#IBOutlet weak var searchBar: UISearchBar!
#IBOutlet weak var locationBtn: UIButton!
#IBOutlet weak var barMapView: MKMapView!
var locationManager = CLLocationManager()
let authorizationStatus = CLLocationManager.authorizationStatus()
let regionRadius: Double = 1000
override func viewDidLoad() {
super.viewDidLoad()
searchBar.isHidden = true
barMapView.delegate = self
locationManager.delegate = self
configureLocationServices()
centerMapOnUserLocation()
}
#IBAction func searchBtnWasPressed(_ sender: Any) {
searchBar.isHidden = false
}
#IBAction func doneBtnWasPressed(_ sender: Any) {
dismiss(animated: true)
}
#IBAction func locationBtnWasPressed(_ sender: Any) {
if authorizationStatus == .authorizedAlways || authorizationStatus == .authorizedWhenInUse {
centerMapOnUserLocation()
}
}
}
extension BarMapVC: MKMapViewDelegate {
func centerMapOnUserLocation() {
guard let coordinate = locationManager.location?.coordinate else { return }
let coordinateRegion = MKCoordinateRegionMakeWithDistance(coordinate, regionRadius * 2.0, regionRadius * 2.0)
barMapView.setRegion(coordinateRegion, animated: true)
}
}
extension BarMapVC: CLLocationManagerDelegate {
func configureLocationServices() {
if authorizationStatus == .notDetermined {
locationManager.requestAlwaysAuthorization()
} else {
return
}
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
centerMapOnUserLocation()
}
}
I'm using CLLocation Manager, complying to mapViewDelegate, as well as have all needed outlets. Anything I' m doing wrong?
You can set this on your map view:
barMapView.showsUserLocation = true

LocationManager singleton didUpdateLocations not getting called

I have created a singleton LocationManager class to handle location updates in my application. In the first view controller the shared instance is accessed in viewDidLoad and a call to getCurrentLocation is made. "Getting users location..." prints to the console however the delegate method didUpdateLocations on the location manager is never called and the location is not updated. I have the required keys in the info.plist and the expected permissions prompts show.
I have a feeling this is a threading issue with the LocationManager falling out of scope but I'm not sure, would be great if someone could point me in the right direction !
Location Manager:
import Foundation
import CoreLocation
class LocationManager: NSObject, CLLocationManagerDelegate {
static let shared = LocationManager()
var manager: CLLocationManager!
override init() {
super.init()
self.manager = CLLocationManager()
self.manager.delegate = self
if CLLocationManager.authorizationStatus() == .notDetermined {
self.manager.requestWhenInUseAuthorization()
}
self.manager.desiredAccuracy = kCLLocationAccuracyBest
}
public func getCurrentLocation(){
print("Getting users location...")
self.manager.startUpdatingLocation()
}
func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) {
self.manager.stopUpdatingLocation()
print("locations = \(locations)")
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error.localizedDescription)
}}
ViewController:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var savedAreasTableView: UITableView!
#IBOutlet weak var noSavedAreasLabel: UILabel!
var manager: LocationManager! = LocationManager.shared
override func viewDidLoad() {
super.viewDidLoad()
self.noSavedAreasLabel.text = "You have no saved areas available !\nTo add some, search for a location and then favourite it."
//Check here to see if user has any favourited areas
self.savedAreasTableView.isHidden = true
self.noSavedAreasLabel.isHidden = false
self.manager.getCurrentLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The correct delegate function is this:
func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation])

Swift 4 CoreLocation not working

why this code does not working? I guess it's something with didUpdateLocations overrated method? How I can correctly connect them with my labels and make them a live :)
import UIKit
import CoreLocation
class MainVC: UIViewController, CLLocationManagerDelegate {
var walking = false
var pause = false
var locationManager: CLLocationManager = CLLocationManager()
var startLocation: CLLocation!
var speed: CLLocationSpeed = CLLocationSpeed()
#IBOutlet weak var latitudeLabel: UILabel!
#IBOutlet weak var longitudeLabel: UILabel!
#IBOutlet weak var horizontalAccuracyLabel: UILabel!
#IBOutlet weak var verticalAccuracyLabel: UILabel!
#IBOutlet weak var distanceLabel: UILabel!
#IBOutlet weak var altitudeLabel: UILabel!
#IBOutlet weak var speedLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
locationManager.startUpdatingHeading()
startLocation = nil
// Ask for Authorisation from the User.
self.locationManager.requestAlwaysAuthorization()
// For use in foreground
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func startDistance(_ sender: UIButton) {
startLocation = nil
walking = true
}
#IBAction func stopDistance(_ sender: UIButton) {
walking = false
}
#IBAction func resetDistance(_ sender: UIButton) {
startLocation = nil
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let latestLocation: AnyObject = locations[locations.count - 1]
latitudeLabel.text = String(format: "%.4f",latestLocation.coordinate.latitude)
longitudeLabel.text = String(format: "%.4f",latestLocation.coordinate.longitude)
horizontalAccuracyLabel.text = String(format: "%.4f",latestLocation.horizontalAccuracy)
verticalAccuracyLabel.text = String(format: "%.4f",latestLocation.verticalAccuracy)
altitudeLabel.text = String(format: "%.4f",latestLocation.altitude)
if walking == true {
if startLocation == nil {
startLocation = latestLocation as! CLLocation
speed = locationManager.location!.speed
speedLabel.text = String(format: "%.0f km/h", speed * 3.6)
}
let distanceBetween: CLLocationDistance =
latestLocation.distance(from: startLocation)
distanceLabel.text = String(format: "%.2f", distanceBetween)
}
}
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading)
{
// capLabel.text = String(format: "%.4f",newHeading.magneticHeading)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
}
}
I also added: Location Always Usage Description in my info.plist
And i allowed Location in the Settings on my simulator too.
Thank you.
To configure always authorization in Swift 4 for location services which I can see you have written in your code self.locationManager.requestAlwaysAuthorization(), do the following:
There is a new key NSLocationAlwaysAndWhenInUsageDescription which is also required in your info.plist
<key>NSLocationAlwaysUsageDescription</key>
<string> location permission text.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string> location permission text.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string> location permission text.</string>
Take a look in the documentation here
You are required to include the NSLocationWhenInUseUsageDescription
and NSLocationAlwaysAndWhenInUsageDescription keys in your app's
Info.plist file. (If your app supports iOS 10 and earlier, the
NSLocationAlwaysUsageDescription key is also required.) If those keys
are not present, authorization requests fail immediately.
So we will have a single prompt now.

location authorization alert showing up after app closes

I created a singleton class to handle location authorization because I needed it for several views in my app. So I created the below Location.swift class.
NOTE: I have added correctly into Info.plist, and have looked at several other posts but none seem to address this (at least none I found)
protocol LocationServiceDelegate {
func tracingLocation(currentLocation: CLLocation)
func tracingLocationDidFailWithError(error: NSError)
}
class Location: NSObject,CLLocationManagerDelegate {
var latitude: Double!
var longitude: Double!
var currentLocation : CLLocation!
var locationManager: CLLocationManager?
var lastLocation: CLLocation?
var delegate: LocationServiceDelegate?
static let sharedInstance:Location = {
let instance = Location()
return instance
}()
override init() {
super.init()
self.locationManager = CLLocationManager()
self.locationManager?.delegate = self
guard let locationManagers = self.locationManager else {
return
}
if CLLocationManager.authorizationStatus() == .notDetermined {
locationManagers.requestWhenInUseAuthorization()
}
locationManagers.desiredAccuracy = kCLLocationAccuracyBest
locationManagers.pausesLocationUpdatesAutomatically = false
locationManagers.distanceFilter = 0.1
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else {
return
}
self.lastLocation = location
updateLocation(currentLocation: location)
}
#nonobjc func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .notDetermined:
locationManager?.requestWhenInUseAuthorization()
break
case .authorizedWhenInUse:
locationManager?.startUpdatingLocation()
break
case .authorizedAlways:
locationManager?.startUpdatingLocation()
break
case .restricted:
// restricted by e.g. parental controls. User can't enable Location Services
break
case .denied:
// user denied your app access to Location Services, but can grant access from Settings.app
break
}
}
// Private function
private func updateLocation(currentLocation: CLLocation){
guard let delegate = self.delegate else {
return
}
delegate.tracingLocation(currentLocation: currentLocation)
}
private func updateLocationDidFailWithError(error: NSError) {
guard let delegate = self.delegate else {
return
}
delegate.tracingLocationDidFailWithError(error: error)
}
func startUpdatingLocation() {
print("Starting Location Updates")
self.locationManager?.startUpdatingLocation()
currentLocation = locationManager?.location
Location.sharedInstance.latitude = currentLocation.coordinate.latitude
Location.sharedInstance.longitude = currentLocation.coordinate.longitude
print(Location.sharedInstance.latitude, Location.sharedInstance.longitude)
// self.locationManager?.startMonitoringSignificantLocationChanges()
}
func stopUpdatingLocation() {
print("Stop Location Updates")
self.locationManager?.stopUpdatingLocation()
}
}
My app is crashing, and I think its because the location authorization is not set in the beginning. The funny thing is that the request alert which prompts the user to allow location services doesn't show up until you leave the app.
Once you close the app and accept the location services, the app works fine. So my question is, why isn't the alert showing up?
it is also interesting to note that this is only occurring through an actual device. In the simulator the alert pops up as expected when the initial view is loading.
my first view that is supposed to load and show data is as follows:
import UIKit
import Alamofire
class CurrentWeatherVC: UIViewController {
#IBOutlet weak var locationLabel: UILabel!
#IBOutlet weak var weatherIcon: UIImageView!
#IBOutlet weak var currentTempLabel: UILabel!
#IBOutlet weak var weatherTypeLabel: UILabel!
var currentWeather : CurrentWeather!
override func viewDidLoad() {
super.viewDidLoad()
Location.sharedInstance.locationManager(manager: Location.sharedInstance.locationManager, didChangeAuthorizationStatus: .authorizedWhenInUse)
currentWeather = CurrentWeather()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
Location.sharedInstance.startUpdatingLocation()
currentWeather.downloadWeatherDetails {
self.updateMainUI()
}
}
func updateMainUI() {
//Double value convterted to string for current temp.
//Added the degree symbol here
//For forecast it gets added in before saved into list so be aware of that.
currentTempLabel.text = "\(currentWeather.currentTemp)°"
weatherTypeLabel.text = currentWeather.weatherType
locationLabel.text = currentWeather.cityName
weatherIcon.image = UIImage(named: currentWeather.weatherType)
}
}
I suspect downloadWeatherDetailss implementation uses a dataTask or one of the other NSURLSession methods that run in background.
Make sure to call UI stuff only on the mainQueue:
// ...
DispatchQueue.main.async {
self.updateMainUI()
}
// ...

Resources