Objective-C OpenWeatherMap API Wrapper – JFOpenWeatherMapManager for iOS and Mac OSX

By | November 2, 2013
JFOpenWeatherMapManagerMultiple

JFOpenWeatherMapManager for iOS provides a quick and easy, drop in component to consume and manipulate the Open Weather Map API directly as Objective-C objects, no casting required. Weather data can be retrieved in a couple of lines of code with the help of a Block.

OpenWeatherMap provide a free* API for downloading the latest weather data, such as temperature, wind speed etc. The API can be consumed in various forms, such as JSON and XML. The API itself can be a little fiddly to get the data out of it that you want, so I put together an Objective-C (iOS & Mac OS X) wrapper called JFOpenWeatherMapManager.

The weather data provided by the API is mapped to Objective-C objects by calling the functions below:

I have provided an example project for JFOpenWeatherMapManager that will allow you to drop a pin on a map and dynamically populate the individual values for temperature etc in a tableview.

JFOpenWeatherMapManager can be found on GitHub.

Objective-C iOS / Mac OS X Wrapper to allow easy manipulation of the Open Weather Map API

Wrapper for Open Weather Map API + MapView example, add a pin to the map to retrieve the current weather data for that location

Add JFWeatherMapManager to your project

Import the following Header files

#import "JFWeatherData.h"
#import "JFWeatherManager.h"
#import "DataModels.h"

If you are using the example project, be sure to set your API key in the View Controller, this is not mandatory and if you choose not to use an API key, simply pass nil to the fetch command later on

static NSString *API_KEY = @"YOUR_API_KEY_HERE";

Create a Reference to a JFWeatherManager object and call the fetch function

JFWeatherManager *weatherManager = [[JFWeatherManager alloc]init];

[weatherManager fetchWeatherDataForLatitude:toAdd.coordinate.latitude andLongitude:toAdd.coordinate.longitude withAPIKeyOrNil:API_KEY :^(JFWeatherData *returnedWeatherData){
NSLog(@"Latitude %.3f",[returnedWeatherData latitudeCoordinateOfRequest]);
NSLog(@"Longitude %.3f",[returnedWeatherData longitudeCoordinateOfRequest]);
NSLog(@"Conditions are %@",[returnedWeatherData currentConditionsTextualDescription]);
NSLog(@"Temperature is %f",[returnedWeatherData temperatureInUnitFormat:kTemperatureCelcius]);
NSLog(@"Sunrise is %@",[returnedWeatherData sunriseTime]);
NSLog(@"Sunset is %@",[returnedWeatherData sunsetTime]);
NSLog(@"Hours of Day Light are %@",[returnedWeatherData dayLightHours]);
NSLog(@"Humidity is %@",[returnedWeatherData humidityPercentage]);
NSLog(@"Pressure is %0.1f",[returnedWeatherData pressureInUnitFormat:kPressureHectopascal]);
NSLog(@"Wind Speed is %0.1f",[returnedWeatherData windSpeedInUnitFormat:kWindSpeedMPH]);
NSLog(@"Wind Direction is %@",[returnedWeatherData windDirectionInGeographicalDirection]);
NSLog(@"Cloud Coverage %@",[returnedWeatherData cloudCovergePercentage]);
NSLog(@"Rainfall Over Next 3h is %0.1fmm",[returnedWeatherData rainFallVolumeOver3HoursInMillimeters]);
NSLog(@"SnowFall Over Next 3h is %0.1fmm",[returnedWeatherData snowFallVolumeOver3HoursInMillimeters]);
}];

The data and format’s that can be returned by the API are:

- (NSString *)currentConditionsTextualDescription; //e.g Broken Clouds
- (double)temperatureInUnitFormat:(TemperatureUnit)temperatureUnit; //e.g 3.0°C , Temperature Available in Kelvin, Celsius and Fahrenheit
- (double)pressureInUnitFormat:(PressureUnit)pressureUnit; //e.g 1007.0 hPA , Pressure Available in Hectopascal, and Pascal
- (NSString *)sunriseTime; //e.g 06:33
- (NSString *)sunsetTime; //e.g 19:32
- (NSString *)dayLightHours; //e.g 12:15
- (NSString *)humidityPercentage; //e.g 88%
- (NSString *)cloudCovergePercentage; //e.g 32%
- (double)windSpeedInUnitFormat:(WindSpeedUnit)windSpeedUnit; //e.g 20.8 MPH, Wind Speed Available in Meters Per Second, Miles Per Hour, Kilometres Per Hour
- (double)windDirectionInDegrees; //e.g 320°
- (NSString *)windDirectionInGeographicalDirection; //e.g N (North)
- (double)rainFallVolumeOver3HoursInMillimeters; //e.g 3mm
- (double)snowFallVolumeOver3HoursInMillimeters; //e.g 7mm
- (double)latitudeCoordinateOfRequest; //e.g 32.79
- (double)longitudeCoordinateOfRequest; //e.g -96.0

Temperature Formats (Kelvin, Celcius, Fahrenheit)

typedef enum temperatureUnits {kTemperatureKelvin, kTemperatureCelcius, kTemperatureFarenheit} TemperatureUnit;

Pressure Formats (Hectopascal , Pascal)

typedef enum pressureUnits {kPressureHectopascal, kPressurePascal} PressureUnit;

Wind Speed Units (MPS,MPH,KPH)

typedef enum windSpeedUnits {kWindSpeedMPS, kWindSpeedMPH, kWindSpeedKPH} WindSpeedUnit;

JFOpenWeatherMapManager can be found on GitHub.

=======================

Attribution (Special Thanks)

OpenWeatherMap API http://openweathermap.org for their great API!
AFNetworking for making the best network library on iOS and Mac OSX (Bundled with this project) http://afnetworking.com

=======================

Contact – Jonathan Field | http://jonathanfield.me

 

4 thoughts on “Objective-C OpenWeatherMap API Wrapper – JFOpenWeatherMapManager for iOS and Mac OSX

  1. Dhairya Shrivastava

    I included JFOpenWeatherMapManager in my project folder and now I am getting an error I believe due to duplicate main – error info. below:

    duplicate symbol _main in:
    /Users/dshrivastava/Library/Developer/Xcode/DerivedData/MyVarTest-dpjozwpcyoinixgtogoipczvdczt/Build/Intermediates/MyVarTest.build/Debug-iphonesimulator/MyVarTest.build/Objects-normal/x86_64/main-EC28EE6BCCFC6868.o
    /Users/dshrivastava/Library/Developer/Xcode/DerivedData/MyVarTest-dpjozwpcyoinixgtogoipczvdczt/Build/Intermediates/MyVarTest.build/Debug-iphonesimulator/MyVarTest.build/Objects-normal/x86_64/main-BEFC1EE9BF5A1E6B.o
    ld: 1 duplicate symbol for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

    Do you have any recommendations for me. Thanks for your help.

    Reply
      1. Dhairya Shrivastava

        Thanks. Got it working last night by deleting some of the example project .m files, storyboard etc. Nice wrapper thanks for putting it together and sharing with others.

        Reply

Leave a Reply to Jon Cancel reply