the making of Bitcoin Ticker app
Click to download project open source on my Github repo :)
It’s simply a bitcoin currency exchange rate app, as a sister app of the weather app from the previous blog. So I will not repeat the in-depth code breakdown since it’s very similar and even simpler.
Here is a snippet of the project:
ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var bitcoinLabel: UILabel!
@IBOutlet weak var currencyLabel: UILabel!
@IBOutlet weak var currencyPicker: UIPickerView!
var coinManager = CoinManager()
override func viewDidLoad() {
super.viewDidLoad()
coinManager.delegate = self
currencyPicker.dataSource = self
currencyPicker.delegate = self
}
}
//MARK: - CoinManagerDelegate
extension ViewController: CoinManagerDelegate {
func didUpdatePrice(price: String, currency:String) {
DispatchQueue.main.async {
self.bitcoinLabel.text = price
self.currencyLabel.text = currency
}
}
func didFailWithError(error: Error) {
print(error)
}
}
//MARK: - UIPickerView DataSource & Delegate
extension ViewController: UIPickerViewDataSource, UIPickerViewDelegate {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
coinManager.currencyArray.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return coinManager.currencyArray[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let selectedCurrency = coinManager.currencyArray[row]
coinManager.getCoinPrice(for: selectedCurrency)
}
}
CoinData.swift
import Foundation
struct CoinData: Codable {
let rate: Double
}
CoinManager.swift
import Foundation
protocol CoinManagerDelegate {
func didUpdatePrice(price: String, currency: String)
func didFailWithError(error: Error)
}
struct CoinManager {
var delegate: CoinManagerDelegate?
let baseURL = "https://rest.coinapi.io/v1/exchangerate/BTC"
let apiKey = "YOUR_API_KEY"
let currencyArray = ["AUD", "BRL","CAD","CNY","EUR","GBP","HKD","IDR","ILS","INR","JPY","MXN","NOK","NZD","PLN","RON","RUB","SEK","SGD","USD","ZAR"]
func getCoinPrice(for currency: String) {
let urlString = "\(baseURL)/\(currency)?apikey=\(apiKey)"
if let url = URL(string: urlString) {
let session = URLSession(configuration: .default)
let task = session.dataTask(with: url) { (data, response, error) in
if error != nil {
self.delegate?.didFailWithError(error: error!)
return
}
if let safeDate = data {
if let bitcoinPrice = self.parseJSON(safeDate) {
let priceString = String(format: "%.2f", bitcoinPrice)
self.delegate?.didUpdatePrice(price: priceString, currency: currency)
}
}
}
task.resume()
}
}
func parseJSON(_ data: Data) -> Double? {
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode(CoinData.self, from: data)
let lastPrice = decodedData.rate
print(lastPrice)
return lastPrice
} catch {
delegate?.didFailWithError(error: error)
return nil
}
}
}