The goal of this project is to use the OpenWeatherMap API to create an app that will display a 7 day forecast for any zip code the user enters.
Please fork and clone this repository. Use the provided Xcode project as it contains some icon images in the Assets folder. Commit regularly as you complete the requirements in this project.
- In a
UIViewController
scene, add aUISearchBar
and aUICollectionView
. - In the collection view cell, add an image view and a label. The image view will show an icon of the weather for the forecast, and the labels will show the temperature and the city respectively.
- Create Cocoa Touch Subclasses for the custom cell and your view controller scene. Set up any outlets and actions necessary.
-
Go to this signup page and sign up for a new OpenWeatherMap account. Once you create your account (and you may have to sign in if it doesn't automatically sign you in), go to the "API keys" section of your "My Home" page. Generate a new API key.
-
Create a model object that represents a daily forecast. It should include:
- The name of the city (yes, this is slightly repetitive)
- The temperature
- A
UIImage
that will be the icon for the forecast (sunny, rainy, etc.)
-
Create a normal memberwise initializer.
-
Create an initializer that takes in a dictionary and the city name. (Refer to the example JSON again, and/or ask a PM if you're unclear on why you should pass the city name in separately)
-
Create a model controller, and add a property of an array of forecasts.
-
In the initializer of the model controller, give an initial value of an empty array for the forecasts property.
-
Add static constants to the model controller for the base url and your API key.
-
Create a function that will perform a data task to get the forecasts. This should take in a zip code, and have a completion block with an error. The process should be the same as if you were writing this in Swift up to the point where you would use a
JSONDecoder
. Instead, useNSJSONSerialization
to turn the data returned from the data task into a dictionary. From there, you will be able to parse it to the point where you can pass a sub-dictionary to your model's initializer.
- In your view controller's .h file, adopt the
UISearchBarDelegate
andUICollectionViewDataSource
protocols. Also add a property for the model controller. - In the view controller's initializers (
initWithCoder
andinitWithNibName
), initialize the model controller property. - In the
viewDidLoad
set the view controller as thedelegate
of the search bar and thedataSource
of the collection view. - Implement the required
UICollectionViewDataSource
methods. - Implement the
searchBarSearchButtonClicked
method to trigger your fetch forecast method from the model controller. Reload the table view on the main queue in the completion block of the fetch forecast method call.
- Add additional properties to the model such as the day of the forecast, highs and lows for the day, wind speed, etc. Update the model's initializers to properly parse the dictionary and initalize the new propeties.
- Change your collection view cell to look more like the common forecast you would see on the news. There are a couple example images included in the repo to help you get some ideas. Feel free to be creative with this.