How to integrate steem-api in swift project Part #1

in #utopian-io8 years ago (edited)

What Will I Learn?

  • You will learn making app in Swift programming language.
  • You will learn how to integrate Steem-api's in swift programming language.
  • You will learn how to integrate Cocoapods in swift programming language.
  • You will learn how to parse Json in swift.

Requirements

  • Xcode
  • Basic Understanding of Xcode.
  • Understanding of swift language in iOS development.
  • Basic knowledge of Cocoapods.

Difficulty

  • Basic

Tutorial Contents

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.
ezgif.com-video-to-gif.gif

  • First of all create a single view app in your Xcode.
  • Then open story board and put one textfields and one button on your view controller. Something like this -
    Screen Shot 2018-03-02 at 10.34.55 PM.png
  • Now make outlets of textfield and button in your viewController file, so that we access them with in our code. Make outlets like this :-

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
    }
}
  • Now we need to make api calls with steem-api to get the details of user. First of all install pod SwiftyJSON in our app because SwiftyJSON makes it easy to deal with JSON data in Swift. To install SwiftyJSON, add the following code in your podfile

 pod 'SwiftyJSON'
  • After installation just use import SwiftyJSON in your view controller.
  • Now we need to fetch the user details on click of 'Go' button click, from steemit throught steem-api. To make api call to steemit, we need to write the following code in our "goButtonClicked" outlet. Like this -:

@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()
    }
  • Now user account details from steemit in 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 :-

Screen Shot 2018-03-02 at 11.33.17 PM.png

  • Ok we added labels to our view controller but currently they are static. We need to change them on click of "Go" button. So for this first make outlets of both labels to viewcontroller and then change them in 'goButtonClicked' Method. The final code in your viewcontroller file had to be 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()
    }
}
  • You see in above code we added the outlets of labels and after this we get username and sbd balance from json dictionary to fill in labels. Now run the app and try with your steemit username.

Note

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.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Thank you.

Hey @iamankit I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Amazing. Please send more for us.

yeah sure...

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by iamankit from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.