A Swift wrapper for the National Weather Service's weather.gov free-to-use public API.
Platform | CI |
---|---|
Apple | |
Linux (Ubuntu & Amazon) |
Linux & AWS support is planned to be dropped in release 0.5. See #12. |
- For Apple-platforms, use Xcode 12. Make sure to test on iOS and macOS.
- For Linux, either:
- Use Codespaces, this repo is setup for Codespaces. If you have access to Codespaces, you can create one using Code → Open with Codespaces.
- Write your code in Xcode, then build and test in Docker. I'd recommend using iainsmith/swift-docker to automate this task.
Swift Package Manager:
https://github.com/WeatherProvider/NationalWeatherService-Swift.git
At this time, I recommend you use exactVersion
or commit
. This library is still under active development and is subject to major changes at any time. I try to minimize API changes between minor versions, but this is a just-in-case.
import NationalWeatherService
import CoreLocation
let nws = NationalWeatherService(userAgent: "(MyWeatherApp, [email protected])")
let location = CLLocation(latitude: 47.6174, longitude: -122.2017)
// Gets the forecast, organized into time periods (such as Afternoon, Evening, etc).
nws.forecast(for: location) { result in
switch result {
case .success(let forecast): print(forecast)
case .failure(let error): print(error)
}
}
// Gets the forecast, organized into hours.
nws.hourlyForecast(for: location) { result in
switch result {
case .success(let forecast): print(forecast)
case .failure(let error): print(error)
}
}
// Gets the current condition.
nws.currentCondition(for: location) { result in
switch result {
case .success(let period): print(period)
case .failure(let error): print(error)
}
}
TODO: this
To do unit tests, you must specify an Environment Variable named NWS_AGENT_CONTACT
with
the value of your email or some other contact information.
The weather data provided by this library is sourced from weather.gov, which is a work of the United States federal government and thus in the public domain.
For more details on the API, see https://www.weather.gov/documentation/services-web-api
This Swift library is licensed under the MIT license.
Copyright © 2020 Alan Chu
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.