/apixu-iOS

Swift library for Apixu Weather API http://www.apixu.com

Primary LanguageSwiftMIT LicenseMIT

APIXU

CI Status Version License Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

iOS 10 *

Installation

APIXU is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'APIXU'

Usage

Access all APIs (search, current, forecast, history) with a query.

    // Initialize with your own API Key
    let apixu: APIXU = APIXU(key: "yourAPIKey")
    
    // New York coordinates
    let coordinates: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 40.730610, longitude: -73.935242)
    
    // String for query
    let city: String = "New York"
    
    // Date in yyyy-MM-dd format
    let date: String = "2019-06-06"

    // Search with coordinates
    apixu.search(matching: .coordinate2D(coordinates)) { [weak self] (result) in
        guard let self = self else { return }
        switch result {
        case .success(let response):
            // [APIXU.SearchResponse] object
            print(response.map({ $0.country })) // list of countries for the given coordinates
        case .failure(let error):
            self.showAlert(with: error) 
        }
    }
    
    // Search with string
    apixu.search(matching: .string(city)) { [weak self] (result) in
        guard let self = self else { return }
        switch result {
        case .success(let response):
            // [APIXU.SearchResponse] object
            print(response.map({ $0.country })) // list of countries for the given string
        case .failure(let error):
            self.showAlert(with: error) 
        }
    }
    
    // Current (also works using string instead of coordinates)
    apixu.current(matching: .coordinate2D(coordinates)) { [weak self] (result) in
            guard let self = self else { return }
            switch result {
            case .success(let response):
                if let errorMessage = response.error?.message {
                    self.showAlert(with: errorMessage)
                } else {
                    // APIXU.Current object
                    print(response.current)
                }
            case .failure(let error):
                self.showAlert(with: error)
            }
    }
      
    // Forecast (also works using string instead of coordinates)
    apixu.forecast(matching: .coordinate2D(coordinates), days: 5) { [weak self] (result) in
        guard let self = self else { return }
            switch result {
            case .success(let response):
                if let errorMessage = response.error?.message {
                    self.showAlert(with: errorMessage)
                } else {
                    // APIXU.Forecast object
                    print(response.forecast)
                }
            case .failure(let error):
                self.showAlert(with: error)
            }
    }
    
    // History (also works using string instead of coordinates)
    apixu.history(matching: .coordinate2D(coordinates), date: date) { [weak self] (result) in
        guard let self = self else { return }
            switch result {
            case .success(let response):
                if let errorMessage = response.error?.message {
                    self.showAlert(with: errorMessage)
                } else {
                    // APIXU.Forecast object
                    /* History weather API method returns historical weather 
                    for a date on or after 1st Jan, 2015. 
                    The data is returned as a Forecast Object.*/
                    print(response.forecast)
                }
            case .failure(let error):
                self.showAlert(with: error)
            }
    }

Query type

    public enum Query {
        /// Latitude, Longitude
        case coordinateString(String, String)

        /// CLLocationCoordinate2D
        case coordinate2D(CLLocationCoordinate2D)

        /// City, zip, metar code, ip
        case string(String)
    }

Models

SearchResponse

Response

Location

ErrorCode

Current

Condition

Forecast

ForecastDay

Day

Astro

Hour

Documentation

Official docs: https://www.apixu.com/doc/

Author

manuelbulos, manuelbulos@gmail.com

License

APIXU is available under the MIT license. See the LICENSE file for more info.