测试已测试 | ✓ |
Lang语言 | Obj-CObjective C |
许可 | MIT |
Released最后发布 | 2017年5月 |
由‘Rain --description=’11 macbook air维护。
这是一个方便地搜索和解析来自 Wunderground 和 Openweathermap 的天气数据的库。
例如,您可以使用 -(void)getPlacemarksFromString:(NSString*)string completion:(SmileWeatherPlacemarksCompletion)completion
按照字符串搜索位置,完成块返回 CLPlacemark
的数组。
[[SmileWeatherDownLoader sharedDownloader] getPlacemarksFromString:@"cupertino" completion:^(NSArray *placeMarks, NSError *error) {
if (!error) {
//search results: array of placemark in here
}
}];
您可以使用 -(void)getWeatherDataFromPlacemark:(CLPlacemark*)placeMark completion:(SmileWeatherDataDownloadCompletion)completion;
来获取位置标记的天气数据,完成块返回格式良好的天气数据 SmileWeatherData。
[[SmileWeatherDownLoader sharedDownloader] getWeatherDataFromPlacemark:placemark completion:^(SmileWeatherData *data, NSError *error) {
if (!error) {
//Well formed weather data in here.
}
}];
通过使用 climacons 字体,SmileWeatherData 包含了与天气图标对应的字符。
SmileWeatherData *data = ...;
UILabel *iconLabel = ...;
//current weather condition
iconLabel.text = data.currentData.icon;
尽可能地将天气信息、时间戳、星期、时区等内容本地化。
SmileWeatherDemoVC
,简单地将天气 Widget 风格 UI 添加到您的项目中。请查看示例项目以获取更多详细信息。
SmileWeatherDemoVC *demoVC = [SmileWeatherDemoVC DemoVCToView:self.containerView];
主要类是 SmileWeatherDownLoader。它负责从 Wunderground 服务器下载数据。它有三个主要方法:
/*!Get weather data from CLPlacemark.*/
-(void)getWeatherDataFromPlacemark:(CLPlacemark*)placeMark completion:(SmileWeatherDataDownloadCompletion)completion;
/*!Get weather data from CLLocation.*/
-(void)getWeatherDataFromLocation:(CLLocation*)location completion:(SmileWeatherDataDownloadCompletion)completion;
/*!Get array of CLPlacemark from the input string.*/
-(void)getPlacemarksFromString:(NSString*)string completion:(SmileWeatherPlacemarksCompletion)completion;
在 SmileWeatherDataDownloadCompletion
块中,返回 SmileWeatherData,其中包含当前天气数据、4天天气预报数据、24小时天气预报数据等。
[[SmileWeatherDownLoader sharedDownloader] getWeatherDataFromPlacemark:placemark completion:^(SmileWeatherData *data, NSError *error) {
if (!error) {
NSLog(@"Current Temperature, Celsius : %@, Fahrenheit: %@", data.currentData.currentTempStri_Celsius, data.currentData.currentTempStri_Fahrenheit);
}
}];
SmileWeatherData 还遵守 NSCoding
,您可以将其存档为 NSData
。
SmileWeatherData *data = ...
NSData* encodedData = [NSKeyedArchiver archivedDataWithRootObject: data];
步骤1. 通过 CocoaPods 使用 SmileWeather。要安装它,只需将以下行添加到您的 Podfile 中
pod 'SmileWeather'
或者,您可以将 SmileWeather
文件夹拖放到您的项目中。
步骤2. 注册 Wunderground 或 Openweathermap 以获取 API 密钥。
步骤3. 按照以下图片添加 API 密钥和 climacons 字体 到您的项目的 Info.plist
中。
如果您使用的是 Wunderground API,请添加此密钥:API_KEY_wunderground
。
如果您使用的是 Openweathermap API,请添加此密钥:API_KEY_openweathermap
。
如果您在 Info.plist
中添加了这两个密钥,请添加 API_NOW
以告知 SmileWeather 应使用哪个,0
代表 API_KEY_wunderground
,而 1
代表 API_KEY_openweathermap
。
步骤4. 最后一步是将 SmileWeatherDownLoader.h
导入到您的项目中,并开始使用它 :)。
感谢 Comyar Zaheri 的 Sol° for iOS,我从这个项目中受到了启发。
感谢 climacons 字体。
SmileWeather 可以在 MIT 许可下使用。有关更多信息,请参阅 LICENSE 文件。