This login request, and a regular post. This relies on a file called SwiftyJSON. I recommend you download it and add it to your project for any JSON parsing. https://github.com/SwiftyJSON/SwiftyJSON. Try downloading a browser extension in chrome called postman to work with JSON and REST. This is an example post request for a login and a generic post request. These go in a modal swift file.
func loginRequest(username: String!, password: String!, completionHandler: ((NSURLResponse!, JSON, NSError?) -> Void)) {
var request : NSMutableURLRequest = NSMutableURLRequest()
let url: String! = "http://yourRestURL/login/\(username)/\(password)"
println(url)
request.URL = NSURL(string: url)
request.HTTPMethod = "POST"
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, data: NSData!, error: NSError!) in
var json = JSON(data: data!)
println(json)
completionHandler(response, json, error)
})
}
func postRequest(resourceURL: String!, completionHandler: ((NSURLResponse!, JSON, NSError?) -> Void)) {
var request : NSMutableURLRequest = NSMutableURLRequest()
request.URL = NSURL(string: resourceURL)
request.HTTPMethod = "POST"
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, data: NSData!, error: NSError!) in
var json = JSON(data: data!)
completionHandler(response, json, error)
})
If you insist on GET: The resource URL is the URL of the get request.
func getRequest(resourceURL: String!, completionHandler: ((NSURLResponse!, JSON, NSError?) -> Void)) {
var request : NSMutableURLRequest = NSMutableURLRequest()
request.URL = NSURL(string: resourceURL)
request.HTTPMethod = "GET"
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, data: NSData!, error: NSError!) in
var json = JSON(data: data!)
completionHandler(response, json, error)
})
}
Usage: In your VC:
func parseLoginRequest (usernameGiven: String, passwordGiven: String, sender: AnyObject?) {
println("parsing")
modal.loginRequest(usernameGiven, password: passwordGiven) { (response, json, error) in
// parse it :)
})