美文网首页
Swift对Alamofire的简单封装

Swift对Alamofire的简单封装

作者: 雪山飞狐_91ae | 来源:发表于2018-07-12 15:13 被阅读544次
    import UIKit
    import Alamofire
    
    enum MethodType {
        case get
        case post
        case put
        case delete
    }
    
    class PDHttp: SessionManager {
    
        static var instance : PDHttp? = nil
        
        class func shareManager() -> PDHttp{
            
            var header : HTTPHeaders = Alamofire.SessionManager.defaultHTTPHeaders
            header["Authorization"] = ""
            header.updateValue("application/json", forKey: "Accept")
            let configration = URLSessionConfiguration.default
            configration.httpAdditionalHeaders = header
            
            instance = PDHttp(configuration: configration)
            
            return instance!
        }
        
        func requestData(_ type : MethodType, urlString : String, parameters : [String : AnyObject]?, success : @escaping (_ responseObject : [String : AnyObject]) -> (), failure : @escaping (_ error : NSError) -> ()) -> (){
            let method : HTTPMethod
            
            switch type {
            case .get:
                method = .get
                break
            case .post:
                method = .post
                break
            case .put:
                method = .put
                break
            default:
                method = .get
            }
            
            self.request(urlString, method: method, parameters: parameters).responseJSON { (response) in
                switch response.result{
                case .success:
                    if let value = response.result.value as? [String : AnyObject]{
                        success(value)
                    }
                case .failure(let error):
                    failure(error as NSError)
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Swift对Alamofire的简单封装

          本文链接:https://www.haomeiwen.com/subject/ykhrpftx.html