tutorial the programming objective language curso apple ios swift

ios - objective - the swift programming language pdf



¿Cómo enviar parámetros POST en Swift? (3)

Aquí un código:

var URL: NSURL = NSURL(string: "http://stackoverflow.com") var request:NSMutableURLRequest = NSMutableURLRequest(URL:URL) request.HTTPMethod = "POST" request.HTTPBody = ?????? NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) in println(NSString(data: data, encoding: NSUTF8StringEncoding)) }

¿Qué debo escribir en request.HTTPBody si deseo enviar la consulta POST "key" = "value" ?


No es diferente de Objective-C, HTTPBody espera un objeto NSData:

var bodyData = "key1=value&key2=value&key3=value" request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);

Tendrá que configurar los valores y las claves usted mismo en la cadena.


espero que sea útil

Importa el archivo alamofire.swift en tu proyecto.

func goLive () { var url:String = "Your url"! url += "user/login" var param = ["email": self.txtmail.text, "password":self.txtpassword.text] request(.POST, url, parameters: param) .responseJSON { (_, _, JSON, _) in //println(JSON) if let gData = JSON as? NSDictionary { println(gData) } else { let alert=UIAlertView() alert.title="Alert" alert.message=gData.valueForKey("message")as? String alert.addButtonWithTitle("OK") alert.show() } } } }


var post:NSString = "api=myposts&userid=/(uid)&page_no=0&limit_no=10" NSLog("PostData: %@",post); var url1:NSURL = NSURL(string: url)! var postData:NSData = post.dataUsingEncoding(NSASCIIStringEncoding)! var postLength:NSString = String( postData.length ) var request:NSMutableURLRequest = NSMutableURLRequest(URL: url1) request.HTTPMethod = "POST" request.HTTPBody = postData request.setValue(postLength, forHTTPHeaderField: "Content-Length") request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") request.setValue("application/json", forHTTPHeaderField: "Accept") var reponseError: NSError? var response: NSURLResponse? var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&reponseError) if ( urlData != nil ) { let res = response as NSHTTPURLResponse!; NSLog("Response code: %ld", res.statusCode); if (res.statusCode >= 200 && res.statusCode < 300) { var responseData:NSString = NSString(data:urlData!, encoding:NSUTF8StringEncoding)! NSLog("Response ==> %@", responseData); var error: NSError? let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as NSDictionary let success:NSInteger = jsonData.valueForKey("error") as NSInteger //[jsonData[@"success"] integerValue]; NSLog("Success: %ld", success); if(success == 0) { NSLog("Login SUCCESS"); self.dataArr = jsonData.valueForKey("data") as NSMutableArray self.table.reloadData() } else { NSLog("Login failed1"); } } else { NSLog("Login failed2"); } } else { NSLog("Login failed3"); }

Esto te ayudará definitivamente