这是一个用于 Forecast.io API 版本 2 的简单 Objective-C 封装。
额外支持功能
代码库包含两个示例
这里有一个非常基础的示例
#import "AppDelegate.h"
#import "Forecast.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[Forecast sharedInstance] initializeWithApiKey:@"FORECAST_APIKEY"];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[TopViewController alloc] init]];
[self.window makeKeyAndVisible];
return YES;
}
使用当前位置获取当前预报数据
#import "TopViewController.h"
#import "Forecast.h"
#import "ForecastApi.h"
#import "ForecastData.h"
@interface TopViewController ()
@end
@implementation TopViewController
- (void)viewDidLoad
{
[super viewDidLoad];
ForecastApi* api = [ForecastApi sharedInstance];
[api getCurrentDataForCurrentLocation:^(ForecastData *data) {
NSLog(@"%@", data.temperature);
NSLog(@"%@", data.icon);
} failure:^(NSError *error) {
NSLog(@"%@", error);
}];
}
@end
使用地址获取当前预报数据
#import "TopViewController.h"
#import "Forecast.h"
#import "ForecastApi.h"
#import "ForecastData.h"
@interface TopViewController ()
@end
@implementation TopViewController
- (void)viewDidLoad
{
[super viewDidLoad];
ForecastApi* api = [ForecastApi sharedInstance];
[api getCurrentDataForAddress:@"yokohama, kanagawa" success:^(ForecastData *data) {
NSLog(@"%@", data.temperature);
NSLog(@"%@", data.icon);
} failure:^(NSError *error) {
NSLog(@"%@", error);
}];
}
@end
使用当前位置获取每日预报数据
#import "TopViewController.h"
#import "Forecast.h"
#import "ForecastApi.h"
#import "ForecastData.h"
@interface TopViewController ()
{
UITableView* _tableView;
NSMutableArray* _dataSource;
}
@end
@implementation TopViewController
- (void)viewDidLoad
{
[super viewDidLoad];
ForecastApi* api = [ForecastApi sharedInstance];
[api getDailyDataForCurrentLocation:^(NSMutableArray *responseArray) {
_dataSource = [responseArray mutableCopy];
[_tableView reloadData];
} failure:^(NSError *error) {
NSLog(@"%@", error);
}];
}
@end
基本上,此代码可用于商业和非商业项目,无需声明。
有关详细信息,请参阅 LICENSE
文件。