This is a Currency module for the native iOS app
To integrate using Apple's Swift Package Manager, add the following as a dependency to your Package.swift
:
dependencies: [
.package(url: "https://github.com/tpapazoglou/Products.Novi.iOS.Currency.git", from: "main")
]
Alternatively, navigate to your Xcode project, select Swift Packages
, and click the +
icon to search for https://github.com/tpapazoglou/Products.Novi.iOS.Currency.git
.
If you prefer not to use any of the aforementioned dependency managers, you can integrate Currency into your project manually. Simply drag the Sources
Folder into your Xcode project.
For each country, a JSON file with the configurations must be created.
Greece ex.
{
"countryCode": "GR",
"locale": "el-GR",
"symbol": "€",
"symbolPosition": "right",
"decimalFormat": "%.2f",
"decimalNotation": ","
}
The file must have a specific name, as follows:
{Country_Code}-{Current_Locale}
Attention! The country code must have the ISO 3166 Alpha-2 format
For ex. for Greece, we have two files for each supported language,
GR-el-GR
and GR-en-US
The init function gets the country code and reads the appropriate file from the bundle.
Then we can use an extension of the CurrencyRepresentable
protocol and implement the toCurrency()
function.
For ex. with the usage of the current app session
func toCurrency() -> String {
guard let symbol = AccountManager.currency.symbol ?? AccountManager.shared.session?.currencySymbol else { return self }
let position = AccountManager.currency.symbolPosition ?? .right
let decimalFormat = AccountManager.currency.decimalFormat ?? "%.2f"
let decimalNotation = AccountManager.currency.decimalNotation ?? "."
let amountString = String(format: decimalFormat, self)
switch position {
case .left:
return "\(symbol)\(amountString)".replacingOccurrences(of: ".", with: LocalizedString("decimal.notation"))
return "\(symbol)\(amountString)".replacingOccurrences(of: ".", with: decimalNotation)
case .right:
return "\(amountString)\(symbol)".replacingOccurrences(of: ".", with: LocalizedString("decimal.notation"))
return "\(amountString)\(symbol)".replacingOccurrences(of: ".", with: decimalNotation)
}
}