Skip to content

imryan/NewsAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NewsAPI

Version License Platform

Example

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

Usage

Initialization

Setup with your API key first.

let news: News = News(apiKey: "your-api-key")

Fetch top headlines

Returns breaking news headlines for a country and category, or currently running on a single or multiple sources.

let options: [QueryOptions] = [
    .country(.us),
    .pageSize(5),
    .sortBy(.popularity)
]
        
news.get(.topHeadlines, with: options, headlinesCompletion: { [weak self] (headlines, error) in
    if let headlines = headlines, let articles = headlines.articles {
	debugPrint(articles)
    }
})

Fetch everything

The NewsAPI indexes every recent news and blog article published by over 50,000 different sources large and small, and you can search through them with this endpoint.

let options: [QueryOptions] = [
    .domains(["bbc.co.uk", "bbc.com"]),
    .excludeDomains(["google.com"]),
    .country(.us)
]
        
news.get(.everything, with: options, headlinesCompletion: { [weak self] (headlines, error) in
    if let headlines = headlines, let articles = headlines.articles {
	debugPrint(articles)
    }
})

Fetch sources

Returns information (including name, description, and category) about the most notable sources the NewsAPI indexes.

let options: [QueryOptions] = [
    .language(.en),
    .category(.technology),
    .country(.us)
]
        
news.get(.sources, with: options, sourcesCompletion: { [weak self] (sources, error) in
    if let sources = sources {
	debugPrint(sources)
    }
})

Query Options

The available query options for filtering results.

/// Keywords or a phrase to search for.
case query(String)

/// Keywords or phrases to search for in the article title only.
case titleQuery(String)

/// The news sources or blogs you want headlines from.
case sources([String])

/// Domains (eg bbc.co.uk, techcrunch.com, engadget.com) to restrict the search to.
case domains([String])

/// Domains (eg bbc.co.uk, techcrunch.com, engadget.com) to remove from the results.
case excludeDomains([String])

/// A date and optional time for the oldest article allowed.
case fromDate(Date)

/// A date and optional time for the newest article allowed.
case toDate(Date)

/// Code of the language you want to get headlines for.
case language(Language)

/// The order to sort the articles in.
case sortBy(SortOptions)

/// The category you want to get headlines for.
case category(Category)

/// The code of the country you want to get headlines for.
case country(Country)

/// The number of results to return per page (request). 20 is the default, 100 is the maximum.
case pageSize(Int)

/// Use this to page through the results if the total results found is greater than the page size.
case page(Int)

Installation

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

pod 'SwiftNewsAPI'

Author

imryan, [email protected]

License

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