Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Mar 22, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 38 additions & 46 deletions OpenWeatherMapAPI/OWMViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
#import "OWMViewController.h"
#import "OWMWeatherAPI.h"

@interface OWMViewController () {
OWMWeatherAPI *_weatherAPI;
NSArray *_forecast;
NSDateFormatter *_dateFormatter;
int downloadCount;
}
@interface OWMViewController ()

@property OWMWeatherAPI *weatherAPI;
@property NSArray *forecast;
@property NSDateFormatter *dateFormatter;

@property NSInteger downloadCount;

@end

@implementation OWMViewController
Expand All @@ -25,28 +25,29 @@ - (void)viewDidLoad
{
[super viewDidLoad];

downloadCount = 0;
self.downloadCount = 0;

NSString *dateComponents = @"H:m yyMMMMd";
NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:[NSLocale systemLocale] ];
_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setDateFormat:dateFormat];
NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:[NSLocale systemLocale]];
self.dateFormatter = [NSDateFormatter new];
self.dateFormatter.dateFormat = dateFormat;

_forecast = @[];
self.forecast = @[];

_weatherAPI = [[OWMWeatherAPI alloc] initWithAPIKey:@"1111111111"]; // Replace the key with your own
self.weatherAPI = [[OWMWeatherAPI alloc] initWithAPIKey:@"1111111111"]; // Replace the key with your own

// We want localized strings according to the prefered system language
[_weatherAPI setLangWithPreferedLanguage];
[self.weatherAPI setLangWithPreferredLanguage];

// We want the temperatures in celcius, you can also get them in farenheit.
[_weatherAPI setTemperatureFormat:kOWMTempCelcius];
self.weatherAPI.measurementSystem = OWMWeatherMeasurementSystemMetrics;

[self.activityIndicator startAnimating];

[_weatherAPI currentWeatherByCityName:@"Odense" withCallback:^(NSError *error, NSDictionary *result) {
downloadCount++;
if (downloadCount > 1) [self.activityIndicator stopAnimating];
[self.weatherAPI currentWeatherByCityName:@"Odense" callback:^(NSError *error, NSDictionary *result) {
self.downloadCount++;
if (self.downloadCount > 1) {
[self.activityIndicator stopAnimating];
}

if (error) {
// Handle the error
Expand All @@ -55,20 +56,19 @@ - (void)viewDidLoad

self.cityName.text = [NSString stringWithFormat:@"%@, %@",
result[@"name"],
result[@"sys"][@"country"]
];
result[@"sys"][@"country"]];

self.currentTemp.text = [NSString stringWithFormat:@"%.1f℃",
[result[@"main"][@"temp"] floatValue] ];
[result[@"main"][@"temp"] floatValue]];

self.currentTimestamp.text = [_dateFormatter stringFromDate:result[@"dt"]];
self.currentTimestamp.text = [self.dateFormatter stringFromDate:result[@"dt"]];

self.weather.text = result[@"weather"][0][@"description"];
}];

[_weatherAPI forecastWeatherByCityName:@"Odense" withCallback:^(NSError *error, NSDictionary *result) {
downloadCount++;
if (downloadCount > 1) [self.activityIndicator stopAnimating];
[self.weatherAPI forecastWeatherByCityName:@"Odense" callback:^(NSError *error, NSDictionary *result) {
self.downloadCount++;
if (self.downloadCount > 1) [self.activityIndicator stopAnimating];

if (error) {
// Handle the error;
Expand All @@ -77,44 +77,36 @@ - (void)viewDidLoad

_forecast = result[@"list"];
[self.forecastTableView reloadData];

}];

[_weatherAPI searchForCityName:@"Buenos Aires" withCallback:^(NSError *error, NSDictionary *result) {
[self.weatherAPI searchForCityName:@"Buenos Aires" callback:^(NSError *error, NSDictionary *result) {
NSLog(@"found: %@", result);
}];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - TableView DataSource

#pragma mark - tableview datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _forecast.count;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.forecast.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"SimpleCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}

NSDictionary *forecastData = [_forecast objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%.1f℃ - %@",
[forecastData[@"main"][@"temp"] floatValue],
forecastData[@"weather"][0][@"main"]
];

forecastData[@"weather"][0][@"main"]];
cell.detailTextLabel.text = [_dateFormatter stringFromDate:forecastData[@"dt"]];

return cell;

}

@end
122 changes: 79 additions & 43 deletions OpenWeatherMapAPI/OpenWeatherMapAPI/OWMWeatherAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,71 +9,107 @@
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

typedef enum {
kOWMTempKelvin,
kOWMTempCelcius,
kOWMTempFahrenheit
} OWMTemperature;
typedef NS_ENUM(NSUInteger, OWMWeatherMeasurementSystem) {
OWMWeatherMeasurementSystemMetrics,
OWMWeatherMeasurementSystemImperial
};

typedef void (^OWMWeatherAPICallback)(NSError* error, NSDictionary *result);

@interface OWMWeatherAPI : NSObject

- (instancetype) initWithAPIKey:(NSString *) apiKey;
/**
* Creates API helper instance with speciefied API-key.
* @param APIKey Represents key used for access to API. http://openweathermap.org/appid
**/
- (instancetype)initWithAPIKey:(NSString *)APIKey;

- (void) setApiVersion:(NSString *) version;
- (NSString *) apiVersion;
/**
* Represents key currently used for request.
*/
@property (nonatomic, readonly) NSString *APIKey;

- (void) setTemperatureFormat:(OWMTemperature) tempFormat;
- (OWMTemperature) temperatureFormat;
/**
* Indicates version currently used for requests.
*/
@property (nonatomic, readonly) NSString *APIVersion;

- (void) setLangWithPreferedLanguage;
- (void) setLang:(NSString *) lang;
- (NSString *) lang;
/**
* Indicates how dates will be represented. If 'YES' NSDate will be used in callbacks. By default 'NO'.
*/
@property (nonatomic) BOOL shouldConvertDates;

#pragma mark - current weather
/**
* Shows which measurement system will used in server responses.
*/
@property (nonatomic) OWMWeatherMeasurementSystem measurementSystem;

-(void) currentWeatherByCityName:(NSString *) name
withCallback:( void (^)( NSError* error, NSDictionary *result ) )callback;
/**
* Shows lang used for forecast weather conditions.
*/
@property (nonatomic) NSString *lang;

/**
* Sets lang according to current user device settings.
**/
- (void)setLangWithPreferredLanguage;

-(void) currentWeatherByCoordinate:(CLLocationCoordinate2D) coordinate
withCallback:( void (^)( NSError* error, NSDictionary *result ) )callback;
#pragma mark - Current weather

-(void) currentWeatherByCityId:(NSString *) cityId
withCallback:( void (^)( NSError* error, NSDictionary *result ) )callback;
- (void)currentWeatherByCityName:(NSString *)name
callback:(OWMWeatherAPICallback)callback;

#pragma mark - forecast
- (void)currentWeatherByCoordinate:(CLLocationCoordinate2D)coordinate
callback:(OWMWeatherAPICallback)callback;

-(void) forecastWeatherByCityName:(NSString *) name
withCallback:( void (^)( NSError* error, NSDictionary *result ) )callback;
- (void)currentWeatherByCityID:(NSString *)cityID
callback:(OWMWeatherAPICallback)callback;

-(void) forecastWeatherByCoordinate:(CLLocationCoordinate2D) coordinate
withCallback:( void (^)( NSError* error, NSDictionary *result ) )callback;
#pragma mark - Forecast

-(void) forecastWeatherByCityId:(NSString *) cityId
withCallback:( void (^)( NSError* error, NSDictionary *result ) )callback;
- (void)forecastWeatherByCityName:(NSString *)name
callback:(OWMWeatherAPICallback)callback;

#pragma mark forcast - n days
- (void)forecastWeatherByCoordinate:(CLLocationCoordinate2D)coordinate
callback:(OWMWeatherAPICallback)callback;

-(void) dailyForecastWeatherByCityName:(NSString *) name
withCount:(int) count
andCallback:( void (^)( NSError* error, NSDictionary *result ) )callback;
- (void)forecastWeatherByCityID:(NSString *)cityID
callback:(OWMWeatherAPICallback)callback;

-(void) dailyForecastWeatherByCoordinate:(CLLocationCoordinate2D) coordinate
withCount:(int) count
andCallback:( void (^)( NSError* error, NSDictionary *result ) )callback;
#pragma mark - Forecast for n days

-(void) dailyForecastWeatherByCityId:(NSString *) cityId
withCount:(int) count
andCallback:( void (^)( NSError* error, NSDictionary *result ) )callback;
- (void)dailyForecastWeatherByCityName:(NSString *)name
withCount:(NSInteger)count
callback:(OWMWeatherAPICallback)callback;

#pragma mark search
- (void)dailyForecastWeatherByCoordinate:(CLLocationCoordinate2D)coordinate
withCount:(NSInteger)count
callback:(OWMWeatherAPICallback)callback;

-(void) searchForCityName:(NSString *)name
withCallback:( void (^)( NSError* error, NSDictionary *result ) )callback;
- (void)dailyForecastWeatherByCityID:(NSString *)cityID
withCount:(NSInteger)count
callback:(OWMWeatherAPICallback)callback;

-(void) searchForCityName:(NSString *)name
withCount:(int) count
andCallback:( void (^)( NSError* error, NSDictionary *result ) )callback;
#pragma mark - Search

- (void)searchForCityName:(NSString *)name
callback:(OWMWeatherAPICallback)callback;

- (void)searchForCityName:(NSString *)name
withCount:(NSInteger)count
callback:(OWMWeatherAPICallback)callback;

#pragma mark - Core

/**
* Calls the web API and converts the result. Then it calls the callback on the caller-queue.
* @param method URL's path.
* @param params Represents key-value pairs fo request.
* @param callback Completion block.
* @warning Should only be used to extend the functionality.
**/
- (void)callMethod:(NSString *)method
withParams:(NSDictionary *)params
callback:(void (^)(NSError *error, NSDictionary *result))callback;

@end
Loading