Created
March 5, 2016 21:33
-
-
Save erikfloresq/4840b0572a2f6f076588 to your computer and use it in GitHub Desktop.
Make a request to API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // make a request | |
| func getListData(parameters : [String:AnyObject], callback: ([NSObject:AnyObject], String?) -> Void) { | |
| var urlService : String! | |
| var parametersInfo : String! | |
| urlService = "\(urlMain)" | |
| parametersInfo = setFormDataFromDictionary(parameters) | |
| let url = NSURL(string: urlService) | |
| let urlRequest = NSMutableURLRequest(URL: url!, cachePolicy: .UseProtocolCachePolicy, timeoutInterval: 30.0) | |
| urlRequest.HTTPMethod = "POST" | |
| urlRequest.addValue(self.token, forHTTPHeaderField: "Authorization") | |
| let requesData:NSData = parametersInfo.dataUsingEncoding(NSUTF8StringEncoding)! | |
| urlRequest.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") | |
| urlRequest.addValue("\(requesData.length)", forHTTPHeaderField: "Content-Length") | |
| urlRequest.HTTPBody = requesData | |
| let session: NSURLSession = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), delegate: nil, delegateQueue: NSOperationQueue.mainQueue()) | |
| let task = session.dataTaskWithRequest(urlRequest) { (data, reponse, error) -> Void in | |
| if error != nil { | |
| //callback("", error?.localizedDescription) | |
| print("Error \(error)") | |
| }else{ | |
| var directionsJson: [NSObject: AnyObject]! | |
| do{ | |
| directionsJson = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! [NSObject:AnyObject] | |
| } catch { | |
| print(error) | |
| } | |
| callback(directionsJson, nil) | |
| } | |
| } | |
| task.resume() | |
| } | |
| // exec func | |
| self.getListData(parameters) { json in | |
| print(json) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment