Skip to content

NewsAPISwift is a Swift client for News API V2, a service that provides breaking news headlines, and search for articles from over 30,000 news sources and blogs.

License

Notifications You must be signed in to change notification settings

lucaslimapoa/NewsAPISwift

Repository files navigation

NewsAPISwift

CI Status Carthage compatible Version License Platform codecov codebeat badge

NewsAPISwift is a Swift client for News API V2, a service that provides breaking news headlines, and search for articles from over 30,000 news sources and blogs.

Usage

NewsAPISwift offers two functions to which sources and top headlines can be requested.

Sources

The first functions is used for listing all available sources indexed by the service. Three parameters can be passed in order to filter results.

Parameter Description
Category The category you would like to get sources for. The possible values are: business, entertainment, general, health, science, sports, technology. Default: all categories.
Language The language you would like to get sources for. The possible values are: ar, de, en, es, fr, he, it, nl, no, pt, ru, se, ud, zh. Default: all languages.
Country The country you would like to get sources for. To get the full list of supported countries, please head to the official documentation. Default: all countries.

Example

import NewsAPISwift

let newsAPI = NewsAPI(apiKey: "YourKeyHere")

newsAPI.getSources(category: .technology, language: .en, country: .us) { result in
    switch result {
    case .success(let sources):
        // Do something with returned sources
    case .failure(let error):
        // Handle error
    }
}

Top Headlines

The second function is used for listing top headlines. It is possible to request headlines for a specific country, a single or multiple sources, as well as keywords.

To get a full list of supported parameters, check the official documentation for the service.

Example

Top Headlines about the weather:

import NewsAPISwift

let newsAPI = NewsAPI(apiKey: "YourKeyHere")

newsAPI.getTopHeadlines(q: "weather") { result in
    switch result {
    case .success(let headlines):
        // Do something with returned headlines
    case .failure(let error):
        // Handle error
    }
}

Top Headlines from BBC News:

newsAPI.getTopHeadlines(sources: ["bbc-news"]) { result in
    switch result {
    case .success(let headlines):
        // Do something with returned headlines
    case .failure(let error):
        // Handle error
    }
}

Top Headlines from the US about technology:

newsAPI.getTopHeadlines(category: .technology, country: .us) { result in
    switch result {
    case .success(let headlines):
        // Do something with returned headlines
    case .failure(let error):
        // Handle error
    }
}

It is also possible to limit the number of articles returned by passing the pageSize parameter. Since there could be more results, pagination is also possible.

newsAPI.getTopHeadlines(pageSize: 20, page: 1) { result in
    switch result {
    case .success(let headlines):
        // Do something with returned headlines
    case .failure(let error):
        // Handle error
    }
}

Example Application

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

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate NewsAPISwift into your Xcode project using CocoaPods, specify it in your Podfile:

platform :ios, '8.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'NewsAPISwift', '~> 2.0'
end

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate NewsAPISwift into your Xcode project using Carthage, specify it in your Cartfile:

github "lucaslimapoa/NewsAPISwift"

Run the following command to build the framework:

carthage update

Then, drag the built NewsAPISwift.framework into your Xcode project.

License

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

NOTE: This library and its author are not endorsed by or affiliated with newsapi.org.

About

NewsAPISwift is a Swift client for News API V2, a service that provides breaking news headlines, and search for articles from over 30,000 news sources and blogs.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published