此项目允许您快速轻松地从 openweathermap.org 获取数据。
获取日预报的方法已经更名。因此,不再是:dailyForecastWeatherByCityName:withCount:withCallback:
,而是现在叫:dailyForecastWeatherByCityName:withCount:andCallback:
添加了新的方法来设置 api 的 lang
参数。
- (void) setLangWithPreferedLanguage;
- (void) setLang:(NSString *) lang;
- (NSString *) lang;
setLangWithPreferedLanguage
方法根据手机上的首选语言设置 lang 参数。
如果你已经安装了 CocoaPods,那么使用 API 非常简单。
将依赖项添加到你的 Podfile
中
pod 'OpenWeatherMapAPI', '~> 0.0.4'
包含头文件 #import "OWMWeatherAPI.h"
。
设置 api
// Setup weather api
OWMWeatherAPI *weatherAPI = [[OWMWeatherAPI alloc] initWithAPIKey:@"YOUR-API-KEY"];
选择默认的温度格式(默认为摄氏度)
[weatherAPI setTemperatureFormat:kOWMTempCelcius];
此 API 在当前只是一个对 http-api 的简单包装。因此,为了获取城市 Odense
的当前天气,你可以按如下方式调用它
[weatherAPI currentWeatherByCityName:@"Odense" withCallback:^(NSError *error, NSDictionary *result) {
if (error) {
// handle the error
return;
}
// The data is ready
NSString *cityName = result[@"name"];
NSNumber *currentTemp = result[@"main"][@"temp"];
}]
结果数据是类似的 NSDictionary
,如下所示(json)
{
coord: {
lon: 10.38831,
lat: 55.395939
},
sys: {
country: "DK",
sunrise: 1371695759, // this is an NSDate
sunset: 1371758660 // this is also converted to a NSDate
},
weather: [
{
id: 800,
main: "Clear",
description: "Sky is Clear",
icon: "01d"
}
],
base: "global stations",
main: {
temp: 295.006, // this is the the temperature format you´ve selected
temp_min: 295.006, // --"--
temp_max: 295.006, // --"--
pressure: 1020.58,
sea_level: 1023.73,
grnd_level: 1020.58,
humidity: 80
},
wind: {
speed: 6.47,
deg: 40.0018
},
clouds: {
all: 0
},
dt: 1371756382,
id: 2615876,
name: "Odense",
cod: 200
}
在 OWMViewController.m
文件中查看示例。
以下方法是当前可用的
按城市名称获取当前天气
-(void) currentWeatherByCityName:(NSString *) name
withCallback:( void (^)( NSError* error, NSDictionary *result ) )callback;
按坐标获取当前天气
-(void) currentWeatherByCoordinate:(CLLocationCoordinate2D) coordinate
withCallback:( void (^)( NSError* error, NSDictionary *result ) )callback;
按城市 ID 获取当前天气
-(void) currentWeatherByCityId:(NSString *) cityId
withCallback:( void (^)( NSError* error, NSDictionary *result ) )callback;
按城市名称获取天气预报
-(void) forecastWeatherByCityName:(NSString *) name
withCallback:( void (^)( NSError* error, NSDictionary *result ) )callback;
按坐标获取天气预报
-(void) forecastWeatherByCoordinate:(CLLocationCoordinate2D) coordinate
withCallback:( void (^)( NSError* error, NSDictionary *result ) )callback;
按城市 ID 获取天气预报
-(void) forecastWeatherByCityId:(NSString *) cityId
withCallback:( void (^)( NSError* error, NSDictionary *result ) )callback;
按城市名称获取日预报
-(void) dailyForecastWeatherByCityName:(NSString *) name
withCount:(int) count
withCallback:( void (^)( NSError* error, NSDictionary *result ) )callback;
按坐标获取日预报
-(void) dailyForecastWeatherByCoordinate:(CLLocationCoordinate2D) coordinate
withCount:(int) count
withCallback:( void (^)( NSError* error, NSDictionary *result ) )callback;
按城市ID每日预报
-(void) dailyForecastWeatherByCityId:(NSString *) cityId
withCount:(int) count
withCallback:( void (^)( NSError* error, NSDictionary *result ) )callback;