The WeatherKit API only needs you to provide a CLLocation object, nothing about the name of a city/country. You will have to use your own list of city/country names and get the locations of these to pass to WeatherKit.
Helpfully, CLGeocoder has that functionality already. Here is an example of how to use it:
func location(for address: String) async throws -> CLLocation? {
let placemarks = try await CLGeocoder().geocodeAddressString(address)
return placemarks.first?.location
}
if let londonLocation = try? await location(for: "London") {
// pass londonLocation to WeatherKit
let coordinates = londonLocation.coordinate
print(coordinates) // CLLocationCoordinate2D(latitude: 51.5033466, longitude: -0.0793965)
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: