获取天气预报从未如此简单,它支持 5 个不同的服务。它返回原始 json,因此您可以轻松获取所需的数据。
AFWeather 可在 CocoaPods 上获得,您可以通过将以下行添加到您的 Podfile 来获得它
pod 'AFWeather', '~> 1.0'
如果您不使用 CocoaPods,您将不得不将这些文件导入到您的项目中
AFWeather.h
AFWeather.m
服务名称 | 地点名称/邮编码 | 坐标 | AFWeather 名称 |
---|---|---|---|
World Weather Online | ![]() |
![]() |
AFWeatherAPIWorldWeatherOnline |
Weather Underground | ![]() |
![]() |
AFWeatherAPIWeatherUnderground |
Open Weather Map | ![]() |
![]() |
AFWeatherAPIOpenWeatherMap |
AccuWeather | ![]() |
![]() |
AFWeatherAPIAccuWeather |
Forecast.io | ![]() |
![]() |
AFWeatherAPIForecast |
首先,请确保您已将主类导入到将要播放音频的类中。
#import "AFWeather.h"
为了让 AFWeather 知道您使用的是哪个服务以及您的 API 密钥,请使用方法 -configureClientWithService:withAPIKey:
。示例
[[AFWeather sharedClient]configureClientWithService:AFWeatherAPIForecast withAPIKey:@"myawesomeapikey"];
为此,请使用 -fetchForecastOfLocationWithName:andCompletionBlock:
示例
[[AFWeather sharedClient]fetchForecastOfLocationWithName:@"Murcia" andCompletionBlock:^(NSDictionary *response, NSError *error) {
if (!error) {
// Handle the response dictionary with all the information
} else {
//Handle the error
}
}];
您还可以使用纬度和经度代替地点名称。
[[AFWeather sharedClient]fetchForecastOfLocationWithLatitude:@"121.518446" andLogitude:@"-26.181156" andCompletionBlock:^(NSDictionary *response, NSError *error) {
if (!error) {
// Handle the response dictionary with all the information
} else {
//Handle the error
}
}];
AFWeather 使用 MIT 许可证,因此请随意使用它!
由 Alvaro Franco 制作。如果您有任何问题,请随时给我留言[email protected]