The entire process to create current application is as follows:
- Create a simple single view application for iOS
- Design the UI
- Close the application
- Install
cocoapods.org
unless you did in the past - cd to your application workspace
- run
pod setup --verbose
- run
pod init
- open your
Podfile
with your editor - Pods
8.1 uncomment the specification
8.2 add required pods
8.2.1 pod 'SwiftyJSON'
8.2.2 pod 'SVProgressHUD'
8.2.3 pod 'Alamofire'
8.3 save
8.4 run
pod install
- open *.xcoprojectworks
- let WeatherViewController to inherit from
CLLocationManagerDelegate
- define
locationManger = CLLocationManager
- in viewDidLoad()
12.1. set
locationManager
toself
12.2. setlocationManager.desiredAccuracy
to your preference or requirement 12.3. setlocationManager.requestWhenInUseAuthorization()
- open
Info.plist
13.1. addPrivacy - Location When In Use Description
13.2. addPrivacy - Location Usage Discription
13.3. we use Open Weather Map viahttp
add the following
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>openweathermap.org</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
- write function
didUpdateLocations
- check last location in
locations
variable - if it owns higher value than 0 then get 'longitude' and 'latitude' variable
stopUpdatingLocation()
and setlocationManager.delegate
tonil
- write function
didFailWithError
- print error
- inform user by updating cityLabel with 'Location's unavailable'
- call
startUpdatingLocation
function inviewDidLoad
function - create a params dictionary variable and save latitude, longitude and appid in it.
- cal getWeatherData and provide
url
andparameters
for it - import
Alamofire
andSwiftyJSON
- complete
Alamofire.request(url, method: .get, parameters: parameters).responseJSON {}
- then check the if
response.result
is success or failure - save the
response.result.value
in an unwraped variable as a JSON type - create a class named
WeatherDataModel
and declare variables as the following list
let/var | name | type | value |
---|---|---|---|
var | temperature | int | 0 |
var | condition | int | 0 |
var | city | String | "" |
var | weatherIconName | String | "" |
- also add a function named
updateWeatherIcon
which implements aswitch
statement to convert code to an icon. - call the
updateUIWithWeatherData
function and set the available data. - Navigating Data Between View Controllers. In
ChangeCityViewController
declare a protocol and defineuserEnteredANewCityName
function that requires a string value. NOTE definition must be before the class definition. - declare an optional
delegate
variable of the created protocol. NOTE by convention the name of this variable isdelegate
. - in
getWeatherPressed
function, assign the text of text field to a variable and if it's notnil
call theuserEneteredANewCityName
function by passing city name ondelegate
variable. - dismiss the current view controller by calling
self.dismiss(animated: true, completion: nil)
function. - in
WeatherViewController
, confirm to inheriteChangeCityDelegate
. - implement
goToChangeCityViewControllerButtonPressed
by callingperformSegue(withIdentifie: "identifier's name", sender: self)
. - implement change city view delegate method, define a dictionary variable and store
"q"
as a city name and app_id. Then callgetWeatherData
. - override
prepare
function for segue and check if thesegue.identifier
is equal to segue's name then createsegue.destination
aviewcontroller
variableas!
ChangeCityViewController
and then assign its delegate toself