Loading data from the AerisWeather API into your application using the iOS SDK is fast and simple. Just create an instance of the AWFWeatherEndpoint
subclass you want to request data from or use the global shared instance, setup your request options and then perform the request.
Each endpoint service class support the same core AWFWeatherEndpoint
methods for requesting data from the API. Some endpoint services, however, may support additional request methods for convenience. Refer to the endpoint services API documentation for the complete list of supported methods.
The following example loads the latest weather observations for Austin, TX:
AWFPlace *place = [AWFPlace placeWithCity:@"austin" state:@"tx" country:@"us"];
[[AWFObservations sharedService] getForPlace:place options:nil completion:^(AWFWeatherEndpointResult * _Nullable result) {
if (result.error) {
NSLog(@"Data failed to load! - %@", result.error.localizedDescription);
return;
}
if ([result.results count] > 0) {
AWFObservation *obs = (AWFObservation *)[result.results firstObject];
NSLog(@"The current weather in %@ is %@ with a temperature of %.0f.", obs.place.name, obs.weather, obs.tempF);
}
}];
let place = AWFPlace(city: "austin", state: "tx", country: "us")
AWFObservations.sharedService().get(forPlace: place, options: nil) { (result) in
guard let results = result?.results else { print("Data failed to load - \(result?.error)"); return }
guard let obs = results.first as? AWFObservation else { return }
print("The current weather in \(place.name) is \(obs.weather) with a temperature of \(obs.tempF).")
}
The following example loads the 7-day extended forecast for Atlanta, GA:
AWFPlace *place = [AWFPlace placeWithCity:@"atlanta" state:@"ga" country:@"us"];
AWFWeatherRequestOptions *options = [AWFWeatherRequestOptions new];
options.limit = 7;
[[AWFForecasts sharedService] getForPlace:place options:options completion:^(AWFWeatherEndpointResult * _Nullable result) {
if (result.error) {
NSLog(@"Data failed to load! - %@", result.error.localizedDescription);
return;
}
if ([result.results count] > 0) {
AWFForecast *forecast = (AWFForecast *)[result.results firstObject];
[forecast.periods enumerateObjectsUsingBlock:^(AWFForecastPeriod *period, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"The forecast for %@ is %@ with a high temperature of %.0f", period.timestamp, period.weather, period.maxTempF);
}];
}
}];
let place = AWFPlace(city: "atlanta", state: "ga", country: "us")
let options = AWFWeatherRequestOptions()
options.limit = 7
AWFForecasts.sharedService().get(forPlace: place, options: options) { (result) in
guard let results = result?.results else { print("Data failed to load - \(result?.error)"); return }
guard let forecast = results.first as? AWFForecast else { return }
forecast.periods?.forEach({ (period) in
print("The forecast for \(period.timestamp) is \(period.weather) with a high temperature of \(period.maxTempF).")
})
}
You also have access to the supported AerisWeather API actions, such as closest, when using endpoints services within the SDK. The following example will load the ten (10) closest official observations for Chicago, IL:
AWFPlace *place = [AWFPlace placeWithCity:@"chicago" state:@"il" country:@"us"];
AWFWeatherRequestOptions *options = [AWFWeatherRequestOptions new];
options.limit = 10;
[[AWFObservations sharedService] getClosestToPlace:place radius:@"50mi" options:options completion:^(AWFWeatherEndpointResult * _Nullable result) {
if (result.error) {
NSLog(@"Data failed to load! - %@", result.error.localizedDescription);
return;
}
[result.results enumerateObjectsUsingBlock:^(AWFObservation *ob, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"The current weather at %@ is %@ with a temperature of %.0f.", ob.place.name, ob.weather, ob.tempF);
}];
}];
let place = AWFPlace(city: "chicago", state: "il", country: "us")
let options = AWFWeatherRequestOptions()
options.limit = 10
let service = AWFObservations()
service.closest(toPlace: place, radius: "50mi", options: options) { (result) in
guard let results = result?.results else { print("Data failed to load - \(result?.error)"); return }
guard let obs = results as? [AWFObservation] else { return }
obs.forEach({ (ob) in
print("The current weather at \(ob.place?.name) is \(ob.weather) with a temperature of \(ob.tempF).")
})
}
Or, allow all observation stations by setting the filterString
option property, including MESONET and personal weather stations and using a separate AWFObservation
instance:
AWFPlace *place = [AWFPlace placeWithCity:@"chicago" state:@"il" country:@"us"];
AWFWeatherRequestOptions *options = [AWFWeatherRequestOptions new];
options.limit = 10;
options.filterString = AWFObservationFilterAll;
AWFObservations *service = [AWFObservations new];
[service getClosestToPlace:place radius:@"50mi" options:options completion:^(AWFWeatherEndpointResult * _Nullable result) {
if (result.error) {
NSLog(@"Data failed to load! - %@", result.error.localizedDescription);
return;
}
[result.results enumerateObjectsUsingBlock:^(AWFObservation *ob, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"The current weather at %@ is %@ with a temperature of %.0f.", ob.place.name, ob.weather, ob.tempF);
}];
}];
let place = AWFPlace(city: "chicago", state: "il", country: "us")
let options = AWFWeatherRequestOptions()
options.limit = 10
options.filterString = AWFObservationFilter.all.rawValue
let service = AWFObservations()
service.closest(toPlace: place, radius: "50mi", options: options) { (result) in
guard let results = result?.results else { print("Data failed to load - \(result?.error)"); return }
guard let obs = results as? [AWFObservation] else { return }
obs.forEach({ (ob) in
print("The current weather at \(ob.place?.name) is \(ob.weather) with a temperature of \(ob.tempF).")
})
}
The same closest action can be used for all other endpoint services, such as requesting the most recent storm reports around a single location. The following example will load all storm reports within 100 miles of Minneapolis for the past four (4) days:
AWFPlace *place = [AWFPlace placeWithCity:@"minneapolis" state:@"mn" country:@"us"];
AWFWeatherRequestOptions *options = [AWFWeatherRequestOptions new];
options.fromDateString = @"-4days";
[[AWFStormReports sharedService] getClosestToPlace:place radius:@"100mi" options:options completion:^(AWFWeatherEndpointResult * _Nullable result) {
if (result.error) {
NSLog(@"Data failed to load! - %@", result.error.localizedDescription);
return;
}
NSLog(@"total reports: %i", [result.results count]);
[result.results enumerateObjectsUsingBlock:^(AWFStormReport *report, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(report);
}];
}];
let place = AWFPlace(city: "minneapolis", state: "mn", country: "us")
let options = AWFWeatherRequestOptions()
options.fromDateString = "-4days"
AWFStormReports.sharedService().closest(toPlace: place, radius: "100mi", options: options) { (result) in
guard let results = result?.results else { print("Data failed to load - \(result?.error)"); return }
guard let reports = results as? [AWFStormReport] else { return }
print("total reports: \(reports.count)")
reports.forEach({ (report) in
print(report)
})
}
Instead of using the closest action, you can also request data from endpoint services for a region defined by specific coordinate bounds:
CLLocationCoordinate2D northwest = CLLocationCoordinate2DMake(51.18, -107.80);
CLLocationCoordinate2D southeast = CLLocationCoordinate2DMake(37.44, -82.05);
AWFCoordinateBounds *bounds = [AWFCoordinateBounds coordinateBoundsWithNorthwest:northwest southeast:southeast];
[[AWFStormReports sharedService] getWithinBounds:bounds options:options completion:^(AWFWeatherEndpointResult * _Nullable result) {
if (result.error) {
NSLog(@"Data failed to load! - %@", result.error.localizedDescription);
return;
}
NSLog(@"total reports: %i", [result.results count]);
[result.results enumerateObjectsUsingBlock:^(AWFStormReport *report, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(report);
}];
}];
let northwest = CLLocationCoordinate2D(latitude: 51.18, longitude: -107.80)
let southeast = CLLocationCoordinate2D(latitude: 37.44, longitude: -82.05)
let bounds = AWFCoordinateBounds(northwest: northwest, southeast: southeast)
AWFStormReports.sharedService().within(bounds: bounds, options: options) { (result) in
guard let results = result?.results else { print("Data failed to load - \(result?.error)"); return }
guard let reports = results as? [AWFStormReport] else { return }
print("total reports: \(reports.count)")
reports.forEach({ (report) in
print(report)
})
}
Last modified: July 30, 2020