Latest 0.2.6
Homepage https://github.com/andreamarcolin/NewsAPIClient
License MIT
Platforms ios 8.0
Authors

CI Status
Version
License
Platform

Requirements

iOS 8.0+
Swift 3.0+

Installation

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

pod "NewsAPIClient"

Usage

First, create a client with your NewsAPI.org API Key

let client = NewsAPIClient(apiKey: "<YOUR_API_KEY>")

The client provides two methods to interact with the two API endpoints: "/sources" and "/articles".
N.B. all operations are performed on the main thread. It is highly reccomended to dispatch each request to a background queue, and pass a completion handler which eventually performs UI updates on the main thread, accordingly with your needs. A general example can be found at http://stackoverflow.com/questions/24056205/how-to-use-background-thread-in-swift

Sources

Send a request to the "/sources" endpoint to get all sources

client.getSources { (sources, error) in
    guard let sources = sources else {
        print(error!)
        return
    }
    print(sources)
}

Optionally filter the sources by category, language or country

client.getSources(category: "business",
                  language: "en",
                  country: "gb")
{ (sources, error) in

    guard let sources = sources else {
        print(error!)
        return
    }

    print(sources)
}

Articles

Normally, chain the sources request with a request to the "/sources" endpoint to get articles from a specific source (sortBy is optional)

client.getSources { (sources, error) in
    guard let sources = sources else {
        print(error!)
        return
    }

    client.getArticles(sourceId: sources[0].id,
                       sortBy: sources[0].availableSortBys["latest"]) // if "latest" is not available for this source, defaults to "top" 
    { (articles, error) in

        guard let articles = articles else {
            print(error!)
            return
        }

        print(articles)
    }
}

Otherwise, if you already have a source object (i.e. previously obtained by the source method) just pass it to the getArticles() method to obtain articles for the given source
Remember, sortby is optional, so in this case we provide an example without passing a sortBy.

client.getArticles(source: source) { (articles, error) in
    guard let articles = articles else {
        print(error!)
        return
    }

    print(articles)
}

Author

Andrea Marcolin, [email protected]

License

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

Latest podspec

{
    "name": "NewsAPIClient",
    "version": "0.2.6",
    "summary": "NewsAPICLient is a REST Client for NewsAPI.org",
    "description": "NewsAPIClient is a REST Client, written in Swift 3.0, which interacts with NewsAPI (https://newsapi.org) to retrieve headlines currently published on a range of news sources and blogs",
    "homepage": "https://github.com/andreamarcolin/NewsAPIClient",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "andreamarcolin": "[email protected]"
    },
    "source": {
        "git": "https://github.com/andreamarcolin/NewsAPIClient.git",
        "tag": "0.2.6"
    },
    "social_media_url": "https://twitter.com/andreamarcolin5",
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "NewsAPIClient/Classes/**/*",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This