Examples
Basic data requests

Basic data requests

Loading data from the Xweather Weather 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 Weather API endpoint documentation (opens in a new tab) for the complete list of supported methods.

Observation for a Single Place

The following example loads the latest weather observations for Austin, TX:

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).")
}

Forecast for a Single Place

The following example loads the 7-day extended forecast for Atlanta, GA:

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).")
    })
}

Nearby Observations

You also have access to the supported Xweather Weather API actions (opens in a new tab), such as closest, when using endpoints services within the SDK. The following example will load the ten (10) closest official observations for Chicago, IL:

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:

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).")
    })
}

Nearby Storm Reports

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:

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)
    })
}

Storm Reports for a Region

Instead of using the closest action, you can also request data from endpoint services for a region defined by specific coordinate bounds:

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)
    })
}