AFNetworking Integrate Swift

AFNetworking Integrate Swift Config , POST , GET , PUT , DELETE , Set Header

aOmMiez
Jun 5, 2014 · 1 min read

Add AFNetworking to Project

https://github.com/AFNetworking/AFNetworking

Add Code in Project-Bridging-Header.h

#import “AFNetworking.h”

Init AFHTTPRequestOperationManager

let manager = AFHTTPRequestOperationManager()

Set Headaer for request

manager.requestSerializer.setValue(“608c6c08443c6d933576b90966b727358d0066b4", forHTTPHeaderField: “X-Auth-Token”)

GET Examples

manager.GET( “http://examples.com/json", 
parameters: nil,
success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
println(“JSON: “ + responseObject.description)
},
failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
println(“Error: “ + error.localizedDescription)
})

POST Examples

var parameters = [“user”:”admin”,”password”:”123456"]
manager.POST( “http://examples.com/login",
parameters: parameters,
success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
println(“JSON: “ + responseObject.description)
},
failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
println(“Error: “ + error.localizedDescription)
})

PUT Examples

var parameters = [“news_id”:1]
manager.PUT( “http://examples.com/",
parameters: parameters,
success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
println(“JSON: “ + responseObject.description)
},
failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
println(“Error: “ + error.localizedDescription)
})

DELETE

var parameters = [“del_id”:1]
manager.DELETE( “http://examples.com/",
parameters: parameters,
success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
println(“JSON: “ + responseObject.description)
},
failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
println(“Error: “ + error.localizedDescription)
})

    aOmMiez

    Written by

    aOmMiez

    Enjoy your work , Don’t waste time. , Be motivated.

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade