Skip to content

Instantly share code, notes, and snippets.

@alimovlex
Last active February 15, 2023 17:53
Show Gist options
  • Select an option

  • Save alimovlex/bd965500737f14989ee4aaea92b87cd2 to your computer and use it in GitHub Desktop.

Select an option

Save alimovlex/bd965500737f14989ee4aaea92b87cd2 to your computer and use it in GitHub Desktop.
The RESTful api demo class for iOS
/*
* Copyright (C) 2023 Recompile.me.
* All rights reserved.
*/
import Alamofire
import Foundation
import SwiftyJSON
typealias DownloadComplete = () -> ();
class CurrentWeather {
let FORECAST_URL = "https://api.openweathermap.org/data/2.5/weather?q=Bern&appid=dd901d59fd590a54f070075a96812a94&units=metric";
func downloadWeatherDetails(completed: @escaping DownloadComplete) {
//Download Current Weather Data
Alamofire.request(FORECAST_URL).responseJSON(queue: .global(qos: .utility)) { result in
if let result = result.data, let formattedJson = try? JSONSerialization.jsonObject(with: result, options: []) as? NSDictionary, let json = try? JSON(data: result) {
print(formattedJson);
print("The country name is: \(json["sys"]["country"].stringValue)", color: .green, global: true);
print("The city name is: \(json["name"].stringValue)", color: .green, global: true);
print("The sunrise is at: \(Date(timeIntervalSince1970: json["sys"]["sunrise"].doubleValue))", color: .green, global: true);
print("The sunset is at: \(Date(timeIntervalSince1970: json["sys"]["sunset"].doubleValue))", color: .green, global: true);
print("The current temperature is: \(json["main"]["temp"].doubleValue.rounded())", color: .green, global: true);
print("The wind speed is: \(json["wind"]["speed"].doubleValue.rounded())", color: .green, global: true);
print("The humidity is: \(json["main"]["humidity"].doubleValue.rounded())", color: .green, global: true);
print("It feels like: \(json["main"]["feels_like"].doubleValue.rounded())", color: .green, global: true);
for (_,value) in json["weather"] {
print("It is : \(value["main"].stringValue) outside", color: .green, global: true);
}
}
completed();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment