iOS 8.0+ Swift 3.0+
NewsAPIClient is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "NewsAPIClient"
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
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)
}
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)
}
Andrea Marcolin, andreamarcolin@hotmail.it
NewsAPIClient is available under the MIT license. See the LICENSE file for more info.