Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize online Weather Forecast by swift

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to achieve online weather forecast by swift". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to achieve online weather forecast by swift".

The function is simple, but a lot of knowledge points are used, such as Wechat search geographical location and so on.

/ / ViewController.swift// WeatherApp// Wenqi pilot training / / bjflexedu.com// qq:376610000import UIKitimport CoreLocationclass ViewController: UIViewController CLLocationManagerDelegate {@ IBOutlet var cityName: UILabel @ IBOutlet var icon: UIImageView @ IBOutlet var tempTxt: UILabel @ IBOutlet var busy: UILabel let locationManage:CLLocationManager = CLLocationManager () override func viewDidLoad () {super.viewDidLoad () busy.startAnimating () let img = UIImage (named: "background.jpg") self.view.backgroundColor = UIColor (patternImage: img) locationManage.delegate = self locationManage.desiredAccuracy = kCLLocationAccuracyBest locationManage.requestAlwaysAuthorization () locationManage.startUpdatingLocation ()} func locationManager (manager: CLLocationManager! DidUpdateLocations locations: AnyObject []!) {var location = locations.count-1] as CLLocation if location.horizontalAccuracy > 0 {let latitude = location.coordinate.latitude let longitude = location.coordinate.longitude self.updateWeatherData (latitude Longitude:longitude) locationManage.stopUpdatingLocation ()}} func updateWeatherData (latitude:CLLocationDegrees,longitude:CLLocationDegrees) {let url = "http://api.openweathermap.org/data/2.5/weather" let params = [" lat ": latitude," lon ": longitude "cnt": 0] let manage = AFHTTPRequestOperationManager () let success = {(operation:AFHTTPRequestOperation! Response:AnyObject!)-> Void in / / println (response) self.busy.stopAnimating () self.busy.hidden = true self.messageInfo.text = nil / / name let jsonResult:NSDictionary = response as NSDictionary self.cityName.text = jsonResult ["name"]? As String var tempNum:Double if let tempValue = jsonResult ["main"]? ["temp"]? As? Double {tempNum = round (tempValue-273.15) self.tempTxt.text = "\ (tempNum)"} let idCon = (jsonResult ["weather"]? As NSArray) [0]? ["id"]? As? Int let nowTime = NSDate (). TimeIntervalSince1970 let sunrise = jsonResult ["sys"]? ["sunrise"]? As? Double let sunset = jsonResult ["sys"]? ["sunset"]? As? Double var nightTime = false//true for night; false for day if nowTime

< sunrise || nowTime >

Sunset {nightTime = true} else {nightTime = false} self.updateIcon Error:NSError!)-> Void in println ("\ (error)") self.messageInfo.text = "remote data not available"} manage.GET (url, parameters: params, success: success, failure: failure)} func updateIcon (condition:Int NightTime:Bool) {if (condition

< 300) { if nightTime { self.icon.p_w_picpath = UIImage(named: "tstorm1_night") } else { self.icon.p_w_picpath = UIImage(named: "tstorm1") } } // Drizzle else if (condition < 500) { self.icon.p_w_picpath = UIImage(named: "light_rain") } // Rain / Freezing rain / Shower rain else if (condition < 600) { self.icon.p_w_picpath = UIImage(named: "shower3") } // Snow else if (condition < 700) { self.icon.p_w_picpath = UIImage(named: "snow4") } // Fog / Mist / Haze / etc. else if (condition < 771) { if nightTime { self.icon.p_w_picpath = UIImage(named: "fog_night") } else { self.icon.p_w_picpath = UIImage(named: "fog") } } // Tornado / Squalls else if (condition < 800) { self.icon.p_w_picpath = UIImage(named: "tstorm3") } // Sky is clear else if (condition == 800) { if (nightTime){ self.icon.p_w_picpath = UIImage(named: "sunny_night") // sunny night? } else { self.icon.p_w_picpath = UIImage(named: "sunny") } } // few / scattered / broken clouds else if (condition < 804) { if (nightTime){ self.icon.p_w_picpath = UIImage(named: "cloudy2_night") } else{ self.icon.p_w_picpath = UIImage(named: "cloudy2") } } // overcast clouds else if (condition == 804) { self.icon.p_w_picpath = UIImage(named: "overcast") } // Extreme else if ((condition >

= 900 & & condition

< 903) || (condition >

) {self.icon.p_w_picpath = UIImage (named: "tstorm3")} / / Cold else if (condition = = 903) {self.icon.p_w_picpath = UIImage (named: "snow5")} / / Hot else if (condition = = 1000) {self. Icon.p_w_picpath = UIImage (named: "sunny")} / / Weather condition is not available else {self.icon.p_w_picpath = UIImage (named: "dunno")}} func locationManager (manager: CLLocationManager! DidFailWithError error: NSError!) {self.messageInfo.text = "Geographic location information cannot be found" println ("Geographic location information cannot be found\ (error)")} override func didReceiveMemoryWarning () {super.didReceiveMemoryWarning () / / Dispose of any resources that can be recreated. }} at this point, I believe you have a deeper understanding of "how to achieve online weather forecast with swift". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report