Prior to iOS 26 I would write something like this:
let location = CLLocation(latitude: coor.latitude, longitude: coor.longitude)
CLGeocoder().reverseGeocodeLocation(location) { placemarks, error in
guard let placemark = placemarks?.first else {
let errorString = error?.localizedDescription ?? "Unexpected Error"
print("Unable to reverse geocode the given location. Error: \(errorString)")
return
}
}
Then I would create a struct like this:
struct ReversedGeoLocation {
let streetNumber: String // eg. 1
let streetName: String // eg. Infinite Loop
let city: String // eg. Cupertino
let state: String // eg. CA
let zipCode: String // eg. 95014
let country: String // eg. United States
let isoCountryCode: String // eg. US
var formattedAddress: String {
return """
\(streetNumber) \(streetName),
\(city), \(state) \(zipCode)
\(country)
"""
}
// Handle optionals as needed
init(with placemark: CLPlacemark) {
self.streetName = placemark.thoroughfare ?? ""
self.streetNumber = placemark.subThoroughfare ?? ""
self.city = placemark.locality ?? ""
self.state = placemark.administrativeArea ?? ""
self.zipCode = placemark.postalCode ?? ""
self.country = placemark.country ?? ""
self.isoCountryCode = placemark.isoCountryCode ?? ""
}
}
Now that Apple has deprecated CLGeocoder and placemarks, I need a way to retrieve all of this structured data. You cannot just take the MKAddress’s fullAddress and try to parse it because it may or may not contain all the relevant fields and that could lead to errors. MKAddressRepresentation also doesn’t give me all the structured data and I’m not sure what to do at this point as a big portion of my project relies on the split out data of the reverse geo.