I’m having trouble getting my Swift application to work with RapidAPI. I’ve tried following the documentation but something isn’t right with my implementation. The API calls don’t seem to be working as expected and I’m not sure what I’m missing.
import UIKit
class WeatherService {
private let apiHeaders = [
"X-RapidAPI-Host": "weatherapi-com.p.rapidapi.com",
"X-RapidAPI-Key": "your-api-key-here"
]
func fetchWeatherData() {
guard let url = URL(string: "https://weatherapi-com.p.rapidapi.com/current.json?q=Paris") else { return }
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "GET"
urlRequest.allHTTPHeaderFields = apiHeaders
urlRequest.timeoutInterval = 15.0
let task = URLSession.shared.dataTask(with: urlRequest) { responseData, httpResponse, requestError in
if let requestError = requestError {
print("Request failed: \(requestError.localizedDescription)")
return
}
if let httpResponse = httpResponse as? HTTPURLResponse {
print("Status code: \(httpResponse.statusCode)")
}
}
task.resume()
}
}
What’s the proper way to structure this code so it actually works with RapidAPI?