In today's tutorial we make Steem Ticker iOS App, we make this app in swift programming language. In this app we gonna show the following details :-
SwiftyJSON because this pod really helps in parsing json. So open your podfile and write the following code :-
pod 'SwiftyJSON'
Save the podfile and got to your terminal and write
pod install
to install pod in your project.
import SwiftyJSON in your view controller.viewcontroller.swift file like this :-
import UIKit
class ViewController: UIViewController {
@IBOutlet var priceLabel: UILabel!
@IBOutlet var lowestAskLabel: UILabel!
@IBOutlet var highestBidLabel: UILabel!
@IBOutlet var percentageChangeLabel: UILabel!
@IBOutlet var steemVolumeLabel: UILabel!
@IBOutlet var sbdVolumeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
import UIKit in your viewcontroller.swift file like this :-
import UIKit
import SwiftyJSON
class ViewController: UIViewController {
@IBOutlet var priceLabel: UILabel!
@IBOutlet var lowestAskLabel: UILabel!
@IBOutlet var highestBidLabel: UILabel!
@IBOutlet var percentageChangeLabel: UILabel!
@IBOutlet var steemVolumeLabel: UILabel!
@IBOutlet var sbdVolumeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
viewDidLoad() function like this :-
import UIKit
import SwiftyJSON
class ViewController: UIViewController {
@IBOutlet var priceLabel: UILabel!
@IBOutlet var lowestAskLabel: UILabel!
@IBOutlet var highestBidLabel: UILabel!
@IBOutlet var percentageChangeLabel: UILabel!
@IBOutlet var steemVolumeLabel: UILabel!
@IBOutlet var sbdVolumeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.getTickerValues()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func getTickerValues() {
let url = URL(string: "https://api.steemjs.com/get_ticker?=value")!
URLSession.shared.dataTask(with: url, completionHandler: {
(data, response, error) in
if(error != nil){
print("error")
}else{
do{
var json = try JSONSerialization.jsonObject(with: data!, options: []) as! [String: AnyObject]
}catch let error as NSError{
print(error)
}
}
}).resume()
}
}
getTickerValues function like this :-
import UIKit
import SwiftyJSON
class ViewController: UIViewController {
@IBOutlet var priceLabel: UILabel!
@IBOutlet var lowestAskLabel: UILabel!
@IBOutlet var highestBidLabel: UILabel!
@IBOutlet var percentageChangeLabel: UILabel!
@IBOutlet var steemVolumeLabel: UILabel!
@IBOutlet var sbdVolumeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.getTickerValues()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func getTickerValues() {
let url = URL(string: "https://api.steemjs.com/get_ticker?=value")!
URLSession.shared.dataTask(with: url, completionHandler: {
(data, response, error) in
if(error != nil){
print("error")
}else{
do{
var json = try JSONSerialization.jsonObject(with: data!, options: []) as! [String: AnyObject]
DispatchQueue.main.async {
self.priceLabel.text = json["latest"] as! String
self.lowestAskLabel.text = json["lowest_ask"] as! String
self.highestBidLabel.text = json["highest_bid"] as! String
self.percentageChangeLabel.text = json["percent_change"] as! String
self.steemVolumeLabel.text = json["steem_volume"] as! String
self.sbdVolumeLabel.text = json["sbd_volume"] as! String
}
}catch let error as NSError{
print(error)
}
}
}).resume()
}
}
Now run the app once and you see all right side labels filled up with the data from steem market. But what about if we need to refresh the data, for now the data only refresh when you run app again. To fix this we need to add a refresh button on our screen. So add a button on your view controller from storyboard like this :-
Now we need to run our api call again on click of "Refresh" button. So first create a action outlet of this button to your viewcontroller.swift and call our getTickerValues function in the button action outlet like this :-
import UIKit
import SwiftyJSON
class ViewController: UIViewController {
@IBOutlet var priceLabel: UILabel!
@IBOutlet var lowestAskLabel: UILabel!
@IBOutlet var highestBidLabel: UILabel!
@IBOutlet var percentageChangeLabel: UILabel!
@IBOutlet var steemVolumeLabel: UILabel!
@IBOutlet var sbdVolumeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.getTickerValues()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func getTickerValues() {
let url = URL(string: "https://api.steemjs.com/get_ticker?=value")!
URLSession.shared.dataTask(with: url, completionHandler: {
(data, response, error) in
if(error != nil){
print("error")
}else{
do{
var json = try JSONSerialization.jsonObject(with: data!, options: []) as! [String: AnyObject]
DispatchQueue.main.async {
self.priceLabel.text = json["latest"] as! String
self.lowestAskLabel.text = json["lowest_ask"] as! String
self.highestBidLabel.text = json["highest_bid"] as! String
self.percentageChangeLabel.text = json["percent_change"] as! String
self.steemVolumeLabel.text = json["steem_volume"] as! String
self.sbdVolumeLabel.text = json["sbd_volume"] as! String
}
}catch let error as NSError{
print(error)
}
}
}).resume()
}
@IBAction func refreshButtonClicked(_ sender: Any) {
self.getTickerValues()
}
}