Skip to content

Commit 196455b

Browse files
committed
Merge pull request #12 from gizmou/master
"Swift Weather Instant" Widget and App Icon
2 parents d396213 + 2920457 commit 196455b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1076
-6
lines changed

Swift Weather Instant/Info.plist

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleDisplayName</key>
8+
<string>Swift Weather Instant</string>
9+
<key>CFBundleExecutable</key>
10+
<string>$(EXECUTABLE_NAME)</string>
11+
<key>CFBundleIdentifier</key>
12+
<string>com.rushjet.Swift-Weather.$(PRODUCT_NAME:rfc1034identifier)</string>
13+
<key>CFBundleInfoDictionaryVersion</key>
14+
<string>6.0</string>
15+
<key>CFBundleName</key>
16+
<string>$(PRODUCT_NAME)</string>
17+
<key>CFBundlePackageType</key>
18+
<string>XPC!</string>
19+
<key>CFBundleShortVersionString</key>
20+
<string>1.0</string>
21+
<key>CFBundleSignature</key>
22+
<string>????</string>
23+
<key>CFBundleVersion</key>
24+
<string>1</string>
25+
<key>NSExtension</key>
26+
<dict>
27+
<key>NSExtensionMainStoryboard</key>
28+
<string>MainInterface</string>
29+
<key>NSExtensionPointIdentifier</key>
30+
<string>com.apple.widget-extension</string>
31+
</dict>
32+
</dict>
33+
</plist>

Swift Weather Instant/MainInterface.storyboard

Lines changed: 206 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//
2+
// Use this file to import your target's public headers that you would like to expose to Swift.
3+
//
4+
5+
#import <AFNetworking/AFNetworking.h>
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
//
2+
// TodayViewController.swift
3+
// Swift Weather Instant
4+
//
5+
// Created by Marc Tarnutzer on 12.12.14.
6+
// Copyright (c) 2014 rushjet. All rights reserved.
7+
//
8+
9+
import UIKit
10+
import NotificationCenter
11+
import CoreLocation
12+
13+
class TodayViewController: UIViewController, NCWidgetProviding, CLLocationManagerDelegate {
14+
15+
let locationManager:CLLocationManager = CLLocationManager()
16+
17+
@IBOutlet weak var time1: UILabel!
18+
@IBOutlet weak var time2: UILabel!
19+
@IBOutlet weak var time3: UILabel!
20+
@IBOutlet weak var time4: UILabel!
21+
@IBOutlet weak var image1: UIImageView!
22+
@IBOutlet weak var image2: UIImageView!
23+
@IBOutlet weak var image3: UIImageView!
24+
@IBOutlet weak var image4: UIImageView!
25+
@IBOutlet weak var temp1: UILabel!
26+
@IBOutlet weak var temp2: UILabel!
27+
@IBOutlet weak var temp3: UILabel!
28+
@IBOutlet weak var temp4: UILabel!
29+
30+
override func viewDidLoad() {
31+
super.viewDidLoad()
32+
33+
locationManager.delegate = self
34+
locationManager.desiredAccuracy = kCLLocationAccuracyBest
35+
36+
if ( ios8() ) {
37+
locationManager.requestAlwaysAuthorization()
38+
}
39+
locationManager.startUpdatingLocation()
40+
}
41+
42+
func updateWeatherInfo(latitude: CLLocationDegrees, longitude: CLLocationDegrees) {
43+
let manager = AFHTTPRequestOperationManager()
44+
45+
let url = "http://api.openweathermap.org/data/2.5/forecast"
46+
println(url)
47+
48+
let params = ["lat":latitude, "lon":longitude]
49+
println(params)
50+
51+
manager.GET(url,
52+
parameters: params,
53+
success: { (operation: AFHTTPRequestOperation!,
54+
responseObject: AnyObject!) in
55+
//println("JSON: " + responseObject.description!)
56+
57+
self.updateUISuccess(responseObject as NSDictionary!)
58+
},
59+
failure: { (operation: AFHTTPRequestOperation!,
60+
error: NSError!) in
61+
println("Error: " + error.localizedDescription)
62+
63+
})
64+
}
65+
66+
func updateUISuccess(jsonResult: NSDictionary) {
67+
68+
if let tempResult = ((jsonResult["list"]? as NSArray)[0]["main"] as NSDictionary)["temp"] as? Double {
69+
// If we can get the temperature from JSON correctly, we assume the rest of JSON is correct.
70+
var temperature: Double
71+
var cntry: String
72+
cntry = ""
73+
74+
if let weatherArray = (jsonResult["list"]? as? NSArray) {
75+
for index in 1...4 {
76+
if let perTime = (weatherArray[index] as? NSDictionary) {
77+
if let main = (perTime["main"]? as? NSDictionary) {
78+
var temp = (main["temp"] as Double)
79+
if (cntry == "US") {
80+
// Convert temperature to Fahrenheit if user is within the US
81+
temperature = round(((temp - 273.15) * 1.8) + 32)
82+
}
83+
else {
84+
// Otherwise, convert temperature to Celsius
85+
temperature = round(temp - 273.15)
86+
}
87+
88+
if (index==1) {
89+
self.temp1.text = "\(temperature)°"
90+
}
91+
if (index==2) {
92+
self.temp2.text = "\(temperature)°"
93+
}
94+
if (index==3) {
95+
self.temp3.text = "\(temperature)°"
96+
}
97+
if (index==4) {
98+
self.temp4.text = "\(temperature)°"
99+
}
100+
}
101+
var dateFormatter = NSDateFormatter()
102+
dateFormatter.dateFormat = "HH:mm"
103+
if let date = (perTime["dt"]? as? Double) {
104+
let thisDate = NSDate(timeIntervalSince1970: date)
105+
let forecastTime = dateFormatter.stringFromDate(thisDate)
106+
if (index==1) {
107+
self.time1.text = forecastTime
108+
}
109+
if (index==2) {
110+
self.time2.text = forecastTime
111+
}
112+
if (index==3) {
113+
self.time3.text = forecastTime
114+
}
115+
if (index==4) {
116+
self.time4.text = forecastTime
117+
}
118+
}
119+
if let weather = (perTime["weather"]? as? NSArray) {
120+
var condition = (weather[0] as NSDictionary)["id"] as Int
121+
var icon = (weather[0] as NSDictionary)["icon"] as String
122+
var nightTime = false
123+
if icon.rangeOfString("n") != nil{
124+
nightTime = true
125+
}
126+
self.updateWeatherIcon(condition, nightTime: nightTime, index: index)
127+
if (index==4) {
128+
return
129+
}
130+
131+
}
132+
}
133+
}
134+
}
135+
}
136+
}
137+
138+
func updatePictures(index: Int, name: String) {
139+
140+
if (index==1) {
141+
self.image1.image = UIImage(named: name)
142+
}
143+
if (index==2) {
144+
self.image2.image = UIImage(named: name)
145+
}
146+
if (index==3) {
147+
self.image3.image = UIImage(named: name)
148+
}
149+
if (index==4) {
150+
self.image4.image = UIImage(named: name)
151+
}
152+
}
153+
154+
func updateWeatherIcon(condition: Int, nightTime: Bool, index: Int) {
155+
// Thunderstorm
156+
157+
var images = [self.image1.image, self.image2.image, self.image3.image, self.image4.image]
158+
159+
if (condition < 300) {
160+
if nightTime {
161+
self.updatePictures(index, name: "tstorm1_night")
162+
} else {
163+
self.updatePictures(index, name: "tstorm1")
164+
}
165+
}
166+
// Drizzle
167+
else if (condition < 500) {
168+
self.updatePictures(index, name: "light_rain")
169+
170+
}
171+
// Rain / Freezing rain / Shower rain
172+
else if (condition < 600) {
173+
self.updatePictures(index, name: "shower3")
174+
}
175+
// Snow
176+
else if (condition < 700) {
177+
self.updatePictures(index, name: "snow4")
178+
}
179+
// Fog / Mist / Haze / etc.
180+
else if (condition < 771) {
181+
if nightTime {
182+
self.updatePictures(index, name: "fog_night")
183+
} else {
184+
self.updatePictures(index, name: "fog")
185+
}
186+
}
187+
// Tornado / Squalls
188+
else if (condition < 800) {
189+
self.updatePictures(index, name: "tstorm3")
190+
}
191+
// Sky is clear
192+
else if (condition == 800) {
193+
if (nightTime){
194+
self.updatePictures(index, name: "sunny_night")
195+
}
196+
else {
197+
self.updatePictures(index, name: "sunny")
198+
}
199+
}
200+
// few / scattered / broken clouds
201+
else if (condition < 804) {
202+
if (nightTime){
203+
self.updatePictures(index, name: "cloudy2_night")
204+
}
205+
else{
206+
self.updatePictures(index, name: "cloudy2")
207+
}
208+
}
209+
// overcast clouds
210+
else if (condition == 804) {
211+
self.updatePictures(index, name: "overcast")
212+
}
213+
// Extreme
214+
else if ((condition >= 900 && condition < 903) || (condition > 904 && condition < 1000)) {
215+
self.updatePictures(index, name: "tstorm3")
216+
}
217+
// Cold
218+
else if (condition == 903) {
219+
self.updatePictures(index, name: "snow5")
220+
}
221+
// Hot
222+
else if (condition == 904) {
223+
self.updatePictures(index, name: "sunny")
224+
}
225+
// Weather condition is not available
226+
else {
227+
self.updatePictures(index, name: "dunno")
228+
}
229+
}
230+
231+
/*
232+
iOS 8 Utility
233+
*/
234+
func ios8() -> Bool {
235+
if ( NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1 ) {
236+
return false
237+
} else {
238+
return true
239+
}
240+
}
241+
242+
//CLLocationManagerDelegate
243+
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
244+
var location:CLLocation = locations[locations.count-1] as CLLocation
245+
246+
if (location.horizontalAccuracy > 0) {
247+
self.locationManager.stopUpdatingLocation()
248+
println(location.coordinate)
249+
updateWeatherInfo(location.coordinate.latitude, longitude: location.coordinate.longitude)
250+
}
251+
}
252+
253+
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
254+
println(error)
255+
}
256+
257+
258+
override func didReceiveMemoryWarning() {
259+
super.didReceiveMemoryWarning()
260+
// Dispose of any resources that can be recreated.
261+
}
262+
263+
func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)!) {
264+
// Perform any setup necessary in order to update the view.
265+
266+
// If an error is encountered, use NCUpdateResult.Failed
267+
// If there's no update required, use NCUpdateResult.NoData
268+
// If there's an update, use NCUpdateResult.NewData
269+
270+
locationManager.startUpdatingLocation()
271+
completionHandler(NCUpdateResult.NewData)
272+
}
273+
274+
}

0 commit comments

Comments
 (0)