0

I had done read the data from API using delegate methods...but i don't understand how to send data back to API


Here is my code to read Data from API..

func getDataFromAPI()
{
    var strApi : String = "http://maps.googleapis.com/maps/api/directions/json?sensor=false&mode=transit&origin=woottonbridge+UK&destination=ashfordinternational+UK&arrival_time=1415358000&alternatives=true"
    var url  = NSURL(string: strApi)
    var request = NSMutableURLRequest(URL: url!)
    request.HTTPMethod = "GET"
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler: {(response : NSURLResponse! , data : NSData! , error : NSError!) -> Void in

   var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
    let jsonDictionary: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary
        if jsonDictionary != nil
        {
           var arrayData : NSArray = jsonDictionary.objectForKey("routes") as NSArray
            println("\(arrayData[0])")
        }
    })
}
ios developer
  • 198
  • 19

2 Answers2

0

Use NSURLConnection with http method "POST", and you can add what ever you want to send to the API inside "BODY" of the request.

Or else as Dato mentioned you can opt for Alamofire, always the best option:)

Look into - How to make an HTTP request in Swift?, How to create and send the json data to server using swift language

All the best

Community
  • 1
  • 1
Suhas Aithal
  • 842
  • 8
  • 20
0

You can try this combination Alamofire and SwiftyJSON.

import UIKit
import Alamofire
import Haneke

class MatchTableViewController: UITableViewController {

    var datas: [JSON] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        Alamofire.request(.GET, "http://maps.googleapis.com/maps/api/directions/json?sensor=false&mode=transit&origin=woottonbridge+UK&destination=ashfordinternational+UK&arrival_time=1415358000&alternatives=true").responseJSON { (request, response, json, error) in
            if json != nil {
                println(json)
            }
        }
    }
}

Reference: http://blog.revivalx.com/2015/02/24/uicollectionview-tutorial-in-swift-using-alamofire-haneke-and-swiftyjson/

Nurdin
  • 23,382
  • 43
  • 130
  • 308