Skip to content

Instantly share code, notes, and snippets.

@lin
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save lin/3c295bc9c6cc1590100b to your computer and use it in GitHub Desktop.

Select an option

Save lin/3c295bc9c6cc1590100b to your computer and use it in GitHub Desktop.

Based on https://medium.com/@aommiez/afnetwork-integrate-swfit-80514b545b40

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)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment