YahooCurrencyApiWrapper 1.1.3

YahooCurrencyApiWrapper 1.1.3

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Apr 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Mohamed Arradi-Alaoui.



Description

YahooCurrencyApiWrapper is a easy API Wrapper from the Yahoo Currency API in order to retrieve data easily

Synopsis

A simple iOS Wrapper for the Yahoo Finance API that helping you :

  1. Getting currency rates from Yahoo Finance API between two currencies

  2. Getting currencies rates from multiples currencies regarding one base currency (for example EUR, GBP, AED from US Dollars in one single request)

  3. Gathering historical currencies rates from the past days (per default it’s USD).

Motivation

I made that project as I really wanted to use currency conversion for a financial project, but I always found API for the stock exchange, but none of them on iOS was enough simple and useful only for currency purposes.

Installation

This library is compatible for iOS 8 + and written in Swift 3.0

Via CocoaPods :

Add the dependency in your podfile as below

pod 'YahooCurrencyApiWrapper'

Then, run the following command:

$ pod install

Code Samples

Currencies rates use case :

let base : String = "USD"
let other : String = "EUR"

YahooCurrencyApiWrapper.sharedInstance.getAllCurrencyFrom(base: base, other: other) { (rate : Double, time : String?, error : NSError?) in

guard error == nil else {

let errorCode : Int = (error?.code)!

switch errorCode { case ErrorType.currencyNotFound.rawValue : print (“currency not found on the market”) case ErrorType.rateNotApplicable.rawValue : print (“rate not applicable for the currency (other)”) default : return } return }

//Everything is fine, we get the rate print (“1 (base) = (rate) (other)”) }

You can have two kind of errors from the Yahoo API (Currency not existing or Rate not found),

All the others related to HTTP Response are handled as an classic NSError type.

public enum ErrorType : Int { case rateNotApplicable = 999,currencyNotFound }

But this not enough, maybe you want to be able to get rates from different currencies regarding the same base currency !

In order to do that you have a specific method for it :

//create a list of currency as [String] 

let others : [String] = ["EUR","GBP","DZD","AED"]

//Then pass it with your base

YahooCurrencyApiWrapper.sharedInstance.getCurrenciesRates(base: base, others: others) { (exchanges : [Exchange]?, error : NSError?) in

guard error == nil else { return}

exchanges?.forEach {
let item : Exchange = $0
print("1 \(item.from!) = \(item.rate!) \(item.to!)")
}

}

It will return you a list of Exchanges object as below , if the currency rate is not available, it will return you a 0.0 value.

public class Exchange {

public var from : String?
public var to : String?
public var rate : Double?

Historitical rates use case :

YahooCurrencyApiWrapper.sharedInstance.getHistoricalCurrencies(currencyName: “GBP”, days: 7) { (items : [HistoricalItem]?, error : NSError?) in

guard error == nil else { return}

items?.forEach { let item : HistoricalItem = $0

print(“open value for (currency) = (item.openValue!) the (item.date)”) } }

This method return a list of item which describe for each day rate for the state (Open, Low, High, Close)

an Historical Item has this structure :

public struct HistoricalItem {

public var openValue : Double? public var highValue : Double? public var lowValue : Double? public var closeValue : Double? public var date : Date?

}

You can checkout the Example App inside the Example folder to use it.

More wille come, but for the basis feature, here we are.

What’s next ?

  1. Provide a way to convert currency directly
  2. Adding cache management

License

The license used is under MIT.

Author

Mohamed ARRADI-ALAOUI - [email protected]

If you are using this library and you think some specific tools can be implemented please feel free to let me know