This repository was archived by the owner on Mar 22, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathOWMViewController.m
More file actions
120 lines (85 loc) · 3.6 KB
/
Copy pathOWMViewController.m
File metadata and controls
120 lines (85 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// OWMViewController.m
// OpenWeatherMapAPI
//
// Created by Adrian Bak on 20/6/13.
// Copyright (c) 2013 Adrian Bak. All rights reserved.
//
#import "OWMViewController.h"
#import "OWMWeatherAPI.h"
@interface OWMViewController () {
OWMWeatherAPI *_weatherAPI;
NSArray *_forecast;
NSDateFormatter *_dateFormatter;
int downloadCount;
}
@end
@implementation OWMViewController
- (void)viewDidLoad
{
[super viewDidLoad];
downloadCount = 0;
NSString *dateComponents = @"H:m yyMMMMd";
NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:[NSLocale systemLocale] ];
_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setDateFormat:dateFormat];
_forecast = @[];
_weatherAPI = [[OWMWeatherAPI alloc] initWithAPIKey:@"1111111111"]; // Replace the key with your own
// We want localized strings according to the prefered system language
[_weatherAPI setLangWithPreferedLanguage];
// We want the temperatures in celcius, you can also get them in farenheit.
[_weatherAPI setTemperatureFormat:kOWMTempCelcius];
[self.activityIndicator startAnimating];
[_weatherAPI currentWeatherByCityName:@"Odense" withCallback:^(NSError *error, NSDictionary *result) {
downloadCount++;
if (downloadCount > 1) [self.activityIndicator stopAnimating];
if (error) {
// Handle the error
return;
}
self.cityName.text = [NSString stringWithFormat:@"%@, %@",
result[@"name"],
result[@"sys"][@"country"]
];
self.currentTemp.text = [NSString stringWithFormat:@"%.1f℃",
[result[@"main"][@"temp"] floatValue] ];
self.currentTimestamp.text = [_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];
if (error) {
// Handle the error;
return;
}
_forecast = result[@"list"];
[self.forecastTableView reloadData];
}];
[_weatherAPI searchForCityName:@"Buenos Aires" withCallback:^(NSError *error, NSDictionary *result) {
NSLog(@"found: %@", result);
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - tableview datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _forecast.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
NSDictionary *forecastData = [_forecast objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%.1f℃ - %@",
[forecastData[@"main"][@"temp"] floatValue],
forecastData[@"weather"][0][@"main"]
];
cell.detailTextLabel.text = [_dateFormatter stringFromDate:forecastData[@"dt"]];
return cell;
}
@end