swift 封装AFN

import UIKit
import AFNetworking

enum WBReqMethod {
    case get
    case post
}

class WBNetworkingManger: AFHTTPSessionManager {

    static let shared : WBNetworkingManger = {
        let manger = WBNetworkingManger()
        manger.responseSerializer = AFHTTPResponseSerializer()
        manger.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return manger
    }()
   

    func request(method:WBReqMethod = .get,urlString:String,prama:[String:AnyObject]?,completion:@escaping (_ jsonString:AnyObject?,_ isSuccess:Bool)->()) {
       
        let successBlock = {(task:URLSessionDataTask,json:Any?) ->() in
            completion(json as AnyObject,true)
        }
       
        let failureBlock = {(task:URLSessionDataTask?,error:Error) ->() in
            completion(nil,false)
        }
       
        if method == .get {
            get(urlString, parameters: prama, progress: nil, success: successBlock, failure: failureBlock)
        }else{
            post(urlString, parameters: prama, progress: nil, success: successBlock, failure: failureBlock)
        }

    }
}

你可能感兴趣的:(swift 封装AFN)