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

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@iamankit·
0.000 HBD
How to integrate steem-api in swift project Part #1
#### 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](https://res.cloudinary.com/hpiynhbhq/image/upload/v1520014933/irgaebufuqyurjgb60mg.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](https://res.cloudinary.com/hpiynhbhq/image/upload/v1520010322/snqcvvixnwknhtxibyr4.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 :-
<pre><code>
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
    }
}
</code></pre>

- 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 <code>podfile</code>
<pre><code>
 pod 'SwiftyJSON'
</code></pre>

- After installation just use <code>import SwiftyJSON</code> 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 -:
<pre><code>
@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()
    }
</code></pre>

- Now user account details from steemit in <code>json</code> 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](https://res.cloudinary.com/hpiynhbhq/image/upload/v1520013818/a9gyegvqtwpr6zcghe8n.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 :-
<pre><code>
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()
    }
}
</code></pre>

- You see in above code we added the outlets of labels and after this we get username and sbd balance from <code>json</code> 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 <code>json</code>, then use breakpoints in your code and try to print json. Thanks.


<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@iamankit/how-to-integrate-steem-api-in-swift-project-part-1">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍 , , , , , , , , , , ,