CocoaPods installation and Alamofire, SwiftyJSON usage with Swift

Hello, I am going to share with you the use of CocoaPods and an example about it in this post.

Sometimes we need different libraries to make the applications more useful which we write.
We can add libraries to our project directly from the terminal with the help of CocoaPods instead of manually adding library to our projects.

Let's create a new xcode project. I called my project CodeBugfix.

We can now close Xcode and start installing CocoaPods and libraries.

 

Let me first explain how to install it

Open the Terminal on Mac and;

  1.  sudo gem install cocoapods
  2. pod setup

With the first command, we install CocoaPods on our machine.
we complete our installation with the second command.

Let's set up the useful libraries, SwiftyJSON and Alamofire.

We open the terminal again and enter the file as cd filename, write the file pwd and check where you are. We can list all the folders by typing ls . Then we write pod init to the terminal and install the Pod file into the project.

We open the file named Podfile in our project and edit it in the note editor as follows:

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'CodeBugfix' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!

# Pods for CodeBugfix

target 'CodeBugfixTests' do
inherit! :search_paths
# Pods for testing
end

target 'CodeBugfixUITests' do
inherit! :search_paths
# Pods for testing
end

pod 'Alamofire', '~> 5.0.0-beta.5'
pod 'SwiftyJSON', '~> 4.0'

end

Here we add our libraries with pod 'Alamofire' and pod ‘SwiftyJSON‘ commands. Now that the Podfile is ready, let's integrate these libraries into our project.

Let's run the following command for this operation.

$ pod install

When the installation process is finished, let's open our project with "CodeBugfix.xcworkspace", which opens the environment in which libraries are integrated, not the "CodeBugfix.xcodeproj" file that opens the Xcode project environment. When we open our project, our libraries are now added under the pod as shown below.

Now we're ready and we use our newly added Alamofire and SwiftyJSON libraries and let us use APIs at https://coinmarketcap.com/api/, which provides us with JSON content. For this process, let's encode the ViewController.swift in our project as follows.

//  ViewController.swift

//  CodeBugfix

//

//  Created by Ümit on 2.06.2019.

//  Copyright © 2019 Ümit. All rights reserved.

//

import UIKit

import SwiftyJSON

import Alamofire

class ViewController: UIViewController {

    let baseUrl = "https://api.coinmarketcap.com/v1/ticker/?convert=EUR&limit=4"

    

    @IBOutlet weak var labelTitle: UILabel!

    @IBOutlet weak var textViewCoins: UITextView!

    

    override func viewDidLoad() {

        super.viewDidLoad()

        getJSONData(url: baseUrl)

    }

    func getJSONData(url: String) {

        

        Alamofire.request(url, method: .get).responseJSON { response in

            if response.result.isSuccess {

                let jsonData : JSON = JSON(response.result.value!)

                print(jsonData)

                self.textViewCoins.text = jsonData.array?.description

                for json in jsonData.arrayValue {

                    print("\(json["name"]) : \(json["price_eur"]) € , \(json["price_usd"]) $")

                    print(json["rank"])

                }

            } else {

                print("Error: \(String(describing: response.result.error))")

            }

        }

        

    }

}

if I summarize what I did in this code:
first, I print the response content in textview.

Then just print the values that come with the for loop below.

For example, I just print Name,Dollar and Euro price of 4 Coins.

Bitcoin : 7763.95073957 € , 8694.42847496 $

1

Ethereum : 240.02818094 € , 268.794576519 $

2

XRP : 0.3941801137 € , 0.4414209879 $

3

Bitcoin Cash : 394.287865216 € , 441.541652911 $
4

 

It's as simple as that. with a few lines of code we read json data and used it.

 

Leave a reply:

Your email address will not be published.

Sliding Sidebar

A few words about me

A few words about me

Software engineer who likes weekend more than weekdays. Loves food and cooks good as well. Loves to travel and gym, can never spend a weekend sitting idle at home. Independent, modern man with a traditional heart. My actions speak more than words.

Social Profiles