Today in this tutorial we will learn how to create a app, which fetch user details from Steemit and show them in app. After following this tutorial to end you have an app in which you can enter steemit username in app and the app tells you details of that user. So let's start creating this beautiful app.
import UIKit
class ViewController: UIViewController {
@IBOutlet var nameTextfield: UITextField!
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.
}
@IBAction func goButtonClicked(_ sender: Any) {
// call steem api in this method
}
}
podfile
pod 'SwiftyJSON'
import SwiftyJSON in your view controller.
@IBAction func goButtonClicked(_ sender: Any) {
let url = URL(string: "https://api.steemjs.com/getAccounts?names[]=" + nameTextfield.text!)!
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()
}
json dictionary and we can access them from their. So now we have to show user details on the app. Here i am going to show username and wallet balance. To show details put some labels under the go button on your view controller like this :-
import UIKit
import SwiftyJSON
class ViewController: UIViewController {
@IBOutlet var nameTextfield: UITextField!
@IBOutlet var userNameLabel: UILabel!
@IBOutlet var userSBDbalanceLabel: 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.
}
@IBAction func goButtonClicked(_ sender: Any) {
let url = URL(string: "https://api.steemjs.com/getAccounts?names[]=" + nameTextfield.text!)!
URLSession.shared.dataTask(with: url, completionHandler: {
(data, response, error) in
if(error != nil){
print("error")
}else{
do{
let json = try JSONSerialization.jsonObject(with: data!, options: []) as! [[String: AnyObject]]
if let name = json[0]["name"] {
DispatchQueue.main.async {
self.userNameLabel.text = name as? String
}
}
if let balance = json[0]["sbd_balance"] {
DispatchQueue.main.async {
self.userSBDbalanceLabel.text = balance as? String
}
}
}catch let error as NSError{
print(error)
}
}
}).resume()
}
}
json dictionary to fill in labels. Now run the app and try with your steemit username.In next part we try to show more details of user like his steemit profile image, reputation, account value, post payouts and much more. If want to check what are you getting from steemit in json, then use breakpoints in your code and try to print json. Thanks.