Swift Libraries.. via Cocoapods

Lots of different abilities available via cocoapods..

To install to your project, open a terminal window

type “cd ” grab the project name from xcode, paste it next to the “cd ” press enter

You are now in the project directory

type “sudo gem install cocoapods”

type your password

Cocoapods should install for this project

For example lets say I want to install alamofire, and swiftyjson…. great ways to access url objects and parse the json that comes back.

In your terminal type “vi podfile”

You should see the editor

You will see something like this

target 'AppName' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!


  pod 'AFNetworking', '~> 2.5'
  pod 'BDBOAuth1Manager'

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

end

type “pod install” . you should see the pods install, and dont forget the include files within your swift code

import Foundation
import UIKit
import Alamofire
import SwiftyJSON
import GooglePlaces

let mapapikey = "YOUR MAP APIKEY"

class UrlSearch{
    
    func SearchMapAPi(latString: String)
    {
        
        //print( "|||||||| ", latString )
        let urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?" + latString + "&radius=500&type=restaurant&key=YOUR MAP APIKEY"
        print( urlString )
        /*let url = URL(string: "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=13.88068,100.43575&radius=300&type=restaurant&key=YOUR MAP APIKEY")!
        */
        let url = URL( string: urlString )
        
        
   
        AF.request(url!).responseJSON { response in
            switch response.result {
                case .success(let value):

                    let swiftyJsonVar = JSON(response.result.value!)
                    let placeArray = swiftyJsonVar["results"]
                

                    var currentPlaceObj :sPlace
                    
                   
                    for i in 0 ..< placeArray.count {
                        
                        let placeName = swiftyJsonVar["results"][i]["name"].string
                        let placeID = swiftyJsonVar["results"][i]["place_id"].string!
                        let placeAddress = swiftyJsonVar["results"][i]["vicinity"].string
                        let lat = swiftyJsonVar["results"][i]["geometry"]["location"]["lat"].double
                        let long = swiftyJsonVar["results"][i]["geometry"]["location"]["lng"].double
                        let locValue:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat!, long! );
                        
                     
                    }
                
                
                
                case .failure(let error):
                    print(error)
                }
           }
        }

The above is a code snippet to get the google places information from the json returned using alamofire and swiftyjson. Please note this is asynchronous

Join the Conversation

2 Comments

  1. Do you have any type of ideas for creating write-ups?

    That’s where I constantly struggle as well as I just end up gazing empty screen for very long time.

Leave a comment

Your email address will not be published. Required fields are marked *