Skip to content

ggsrivas/CityWeatherIOS

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 

Repository files navigation

CityWeatherIOS

IOS Weather App Example app showing how to work wtih API, CoreData and blocks. Components:

Program Structure

The program has three parts:

  • Weather class, manages Core Data
  • Utilities class, provides accessors for API call.
  • ViewController, manages the view and data. Uses GCD to update UX in main thread.

Global

  • Constants.h defines all the constants used globall. To control the debug output set the appropriate flags.

CoreData

There is only one Entity in Core Data called Weather. You can see it in file WeatherAPIExample.xcdatamodeld on file navigator menu. It is a basic one tabular representation of the data provided by OpenWeather API. To use your own API you would need to get you own API Key called APPID from your settings. The public interface from Core Data Weather class is managed by Weather class extension Weather+OpenWeather The interface methods are:

  • (Weather *) weatherWithOpenWeatherInfo: (NSDictionary *) weatherDataDictionary inCityCountry: (NSString *) cityCountry inAppDelegate: (AppDelegate *) appDelegate;

  • (Weather *) weatherWithCityCountry: (NSString *) cityCountry inAppDelegate: (AppDelegate *) appDelegate;

  • (NSString *) weatherIconImageName: (NSString *) iconName;

API

The data is fetched from OpenWeather Map servers. The call is using HTTP. By default IOS9 turns off HTTP so we need to make an exception in info.plist

  • NSAppTransportSecurity App Transport Security Settings - Dictionary
  • NSAllowsArbitraryLoad Allow Arbitray Loads = YES

The Utilities class method is used to downlaod the weather data in background. The method used is:

  • (void) downloadWeatherDataFromAPIForCityCountry: (NSString *) cityCountry successHandler: (void(^) (NSDictionary * data)) successHandler errorHandler: (void(^) (NSString * errorMessage)) errorHandler;
  • successHandler is called by downloadWeatherDataFromAPIForCityCountry and is passed yje JSON data dictionary fecthed from the server. The successHandler saves the data in dictionary to the Core Data Entity Weather and also updates the UX in the UI thread.
  • errorHandler is invoked if there is an error with a error message passed in arguments.
  • Note: Check for how self is handled in the successHandler and errorHandler callback blocks to avoid memory leakage.

UX

The UX manages starts the program. The steps are:

  • The user enters a city,country in the text field
  • The user then presses the Get Weather button
  • The button handler then checks for the city,country data in the Weather Entity in Core Data. If there us less than 20 minutes old weather data avaialble then it it shown to the user.
  • If the data is not there or is older than 20 minutes in Core Data then a fresh fetch is initiated to the server.
  • When data is received then it saved in Core Data with timestamp and then UI is updated.
  • If not data is received or there is an error error message alert box is shown.

About

IOS Weather App

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Objective-C 100.0%