get the current city from coordinates iOS swift

Solution:

In iOS you can get the current city from your coordinates using the below function.

Code:


    func getCurrentCity(loc:CLLocation , completion: @escaping (_ result: String) -> Void,
                        failure: (_ err:NSError) -> Void) {
        // Get user's current location name
        let geocoder = CLGeocoder()
        var placeMark:String = ""
        geocoder.reverseGeocodeLocation(loc) { (placemarksArray, error) in
                          if (placemarksArray?.count)! > 0 {
                    // City
                    
                    let placemark = placemarksArray?.first
                    
                    if let city = placemark?.addressDictionary!["City"] as? NSString {
                        print(city)
                    }
                    placeMark =  placemark!.locality!
                    completion(placeMark)
            }
        }
    }

You can get the location data you must have to check that if Location service is on and internet connection is on.

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift