刚写swift的时候,用的是Alamofire,使用一段时间后,上网查了下NSURLSession的原理,并结合前人技术博客,就自己写了个工具类。。。
首先,定义一个网络请求的枚举类型,定义枚举类型,是怕有时候不小心,字符串写错了
/**
enum HTTPMethod: String {
case GET = "GET"
case POST = "POST"
case PUT = "PUT"
case DELETE = "DELETE
}
其实我们也可以按照下面的写法,
* 不需要把和键命相同的值字符串写出来
*/
enum HTTPMethod: String {
case GET, POST, PUT, DELETE
}
第二步:定义一个 NetworkManager 的结构体
{
let method: HTTPMethod
let url: String
let params: [String: AnyObject]?
let callback: (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void
private(set) var request = NSMutableURLRequest()
虽然结构体不需要写初始化方法,可是有时我们只初始化部分属性的话,还是的自己写一个。。。同时,如果你定义属性的时候,没有初始化,只是给属性指定类型的话,结构体的初始化方法必须包含那个属性。
如request属性,我已经在定义的时候给它初始化了,下面结构体的初始化也就不需要了
init(method: HTTPMethod, url: String, params: [String: AnyObject]?, callback: (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void) {
self.url = url
self.method = method
self.params = params
self.callback = callback
}
然后是创建网络请求request
/**
创建request
*/
private mutating func buildRequest() {
var newUrl = url
if method == .GET, let myParams = params where myParams.count > 0 {
newUrl += "?" + buildParams(myParams)
}
guard let URL = NSURL(string: newUrl) else {
return
}
request = NSMutableURLRequest(URL: URL)
request.HTTPMethod = method.rawValue
}
创建请求体
/**
创建请求体
*/
private func buildBody() {
guard method != .GET else {
return
}
guard let myParams = params where
myParams.count > 0 else {
return
}
request.HTTPBody = buildParams(myParams).dataUsingEncoding(NSUTF8StringEncoding)
}
/**
创建并开启任务
*/
private func fireTask() {
let session = NSURLSession.sharedSession()
session.dataTaskWithRequest(request) { (data, response, error) in
self.callback(data: data, response: response, error: error)
}.resume()
}
然后写一个方法,包含了上面的3歌步骤
/**
一整套流程
*/
mutating func start() {
buildRequest()
buildBody()
fireTask()
}
上面创建request和请求体的方法里从Alamofire里提取了3个方法
/**
请求参数拼接
*/
func buildParams(parameters: [String: AnyObject]) -> String {
var components: [(String, String)] = []
for key in parameters.keys.sort(<) {
let value = parameters[key]!
components += queryComponents(key, value)
}
return (components.map { "\($0)=\($1)" } as [String]).joinWithSeparator("&")
}
func queryComponents(key: String, _ value: AnyObject) -> [(String, String)] {
var components: [(String, String)] = []
if let dictionary = value as? [String: AnyObject] {
for (nestedKey, value) in dictionary {
components += queryComponents("\(key)[\(nestedKey)]", value)
}
} else if let array = value as? [AnyObject] {
for value in array {
components += queryComponents("\(key)[]", value)
}
} else {
components.append((escape(key), escape("\(value)")))
}
return components
}
func escape(string: String) -> String {
let generalDelimitersToEncode = ":#[]@" // does not include "?" or "/" due to RFC 3986 - Section 3.4
let subDelimitersToEncode = "!$&'()*+,;="
let allowedCharacterSet = NSCharacterSet.URLQueryAllowedCharacterSet().mutableCopy() as! NSMutableCharacterSet
allowedCharacterSet.removeCharactersInString(generalDelimitersToEncode + subDelimitersToEncode)
var escaped = ""
if #available(iOS 8.3, OSX 10.10, *) {
escaped = string.stringByAddingPercentEncodingWithAllowedCharacters(allowedCharacterSet) ?? string
} else {
let batchSize = 50
var index = string.startIndex
while index != string.endIndex {
let startIndex = index
let endIndex = index.advancedBy(batchSize, limit: string.endIndex)
let range = startIndex..
至此管理类已经创建完成。下面开始写具体的get和post请求。
字数太多大家都不愿意看,我把它分成了2篇来写
http://www.jianshu.com/p/5c8200e3c09a