这是一个简单的 iOS 和 OS X 库,用于通过使用 Weather Underground API 获取天气信息
您需要一个 Weather Underground API key 来开始。
//Get the location
CLGeocoder *geocoder = [CLGeocoder new];
[geocoder geocodeAddressString:@"New York" completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks firstObject];
//Initialize request
MKWeatherRequest *request = [MKWeatherRequest requestWithType:MKWeatherRequestTypeCurrentConditions
location:placemark.location];
request.weatherUndergroundApiKey = @"API_KEY_HERE";
[request performRequestWithHandler:^(NSError *error, id responseObject) {
MKWeatherCondition *currentConditions = responseObject;
//Use currentConditions object here
}];
}];
CLGeocoder *geocoder = [CLGeocoder new];
[geocoder geocodeAddressString:@"San Francisco" completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks firstObject];
MKWeatherRequest *request = [MKWeatherRequest requestWithType:MKWeatherRequestTypeHourly
location:placemark.location];
request.weatherUndergroundApiKey = @"API_KEY_HERE";
[request performRequestWithHandler:^(NSError *error, id responseObject) {
NSArray *hours = responseObject;
//Array of MKWeatherCondition Objects
}];
}];
CLGeocoder *geocoder = [CLGeocoder new];
[geocoder geocodeAddressString:@"London, England" completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks firstObject];
MKWeatherRequest *request = [MKWeatherRequest requestWithType:MKWeatherRequestType3DayForecast
location:placemark.location];
request.weatherUndergroundApiKey = @"API_KEY_HERE";
[request performRequestWithHandler:^(NSError *error, id responseObject) {
NSArray *threeDayForecast = responseObject;
//Here responseObject is an array that contains 4 objects: tonight, and the next three days.
MKWeatherCondition *tonightsConditions = [threeDayForecast firstObject];
NSLog(@"%f", tonightsConditions.highTemp.c);
NSLog(@"%f", tonightsConditions.lowTemp.c);
NSLog(@"%ld", (long)tonightsConditions.averageHumudity);
}];
}];
CLGeocoder *geocoder = [CLGeocoder new];
[geocoder geocodeAddressString:@"New Mexico" completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks firstObject];
MKWeatherRequest *request = [MKWeatherRequest requestWithType:MKWeatherRequestType3DayForecastSummary
location:placemark.location];
request.weatherUndergroundApiKey = @"API_KEY_HERE";
[request performRequestWithHandler:^(NSError *error, id responseObject) {
NSArray *threeDayForecast = responseObject;
//Here responseObject is an array which contains 8 objects
MKWeatherCondition *condition = [threeDayForecast firstObject];
NSLog(@"%@", condition.generalTimeOfDayTitle);
NSLog(@"%@", condition.fullSummaryC);
NSLog(@"%@", condition.fullSummaryF);
}];
}];
MKWeatherCondition
对象有一个 climacon
属性,其中包含适合天气条件描述的 climacon 字符映射。Climacons 字体是由 Adam Whitcroft 创建的字体集,包括各种与天气相关的图标。为了使用 climacon
属性,请下载 Climacons 字体并将其添加到您的项目中。
此库使用 Core Location
来执行请求。
MKWeatherCondition
- 通用天气条件
MKWeatherParser
- 解析从 Weather Underground API 收到的天气信息
MKWeatherRequest
- 表示对 Weather Underground API 的单个请求
Kevin Mindeguia 提供的 NSDictionary/NSArray 分类。
Comyar Zaheri 提供的 Climacons.h
文件和 Climacons
描述。
查看 License
文件