-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
90 lines (75 loc) · 2.75 KB
/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
// Weather App
//
// Created by deathlezz on 17/08/2021.
//
// Targets -> Signing & Capabilities -> Location
// System Preferences -> Security & Privacy -> Enable Location Services
import CoreLocation
// specify how the JSON file looks like
struct Json: Codable {
let name: String
let main: [String: Double]
let wind: [String: Double]
}
// create parse json function
func parseJSON(url: String) {
do {
// create url
let url = URL(string: url)
// make http (get) call
let contents = try String(contentsOf: url!)
// specify decoding format
let data = contents.data(using: .utf8)
// decode json data
let weatherData = try JSONDecoder().decode(Json.self, from: data!)
// output
print("City: \(weatherData.name)")
print("Temperature: \(Int(weatherData.main["temp"]! - 273.15)) °C")
print("Feels like: \(Int(weatherData.main["feels_like"]! - 273.15)) °C")
print("Wind: \(Int(weatherData.wind["speed"]! * 3.6)) km/h")
print("Pressure: \(Int(weatherData.main["pressure"]!)) hPa")
print("Humidity: \(Int(weatherData.main["humidity"]!)) %")
} catch {
// error handling
print("* City not found *")
}
}
// create location manager function
func locationManager(_ manager: CLLocationManager, _ status: CLAuthorizationStatus) {
// enable location service
let manager = CLLocationManager()
// get user current location
manager.startUpdatingLocation()
// one second delay
sleep(1)
// get user current location coordinates
let location = manager.location!.coordinate
// call the function
parseJSON(url: "http://api.openweathermap.org/data/2.5/weather?lat=\(location.latitude)&lon=\(location.longitude)&appid=410d1a69fc7442938ca824dce73c9cdf")
}
print("* Welcome to Weather App *")
print()
print("Select operation:")
print("[1] Show current weather for your location")
print("[2] Show current weather for specific city")
let input = Int(readLine()!)
if input == 1 {
// call the function
locationManager(CLLocationManager(), CLAuthorizationStatus.authorized)
} else if input == 2 {
print("Enter city:")
// city input
let city = readLine()
// check if city doesn't start and doesn't end with whitespace
if !city!.hasPrefix(" ") && !city!.hasSuffix(" ") {
// replace whitespace between words with "%20" (e.g. New York = New%20York)
let replace = city!.replacingOccurrences(of: " ", with: "%20")
// call the function
parseJSON(url: "http://api.openweathermap.org/data/2.5/weather?q=\(replace)&appid=410d1a69fc7442938ca824dce73c9cdf")
} else {
print("* Avoid whitespaces *")
}
} else {
print("* Enter 1 or 2 *")
}