美文网首页
swift 使用 AFN 上传 json 参数

swift 使用 AFN 上传 json 参数

作者: 孤雁_南飞 | 来源:发表于2018-07-23 17:59 被阅读19次

    import UIKit

    class HCNetWork: AFHTTPSessionManager {

    //单利
    
    staticletsharedTools:HCNetWork= {
    
        letbaseUrl =NSURL(string:"http://www.51xianchang.com/")!
    
        letmanager =HCNetWork.init(baseURL: baseUrlasURL, sessionConfiguration:URLSessionConfiguration.default)
    
        manager.responseSerializer.acceptableContentTypes = NSSet(objects: "application/json", "text/html", "text/json", "text/javascript", "text/plain") as? Set
    
        // 设置请求参数为JSON字符串
    
        manager.requestSerializer = AFJSONRequestSerializer()
    
        // 请求超时设定
    
        manager.requestSerializer.timeoutInterval = 10
    
        // 设置允许自生成证书
    
        manager.securityPolicy.allowInvalidCertificates = true
    
        //设置请求头
    
        manager.requestSerializer.setValue("APP", forHTTPHeaderField:"loginType")
    
        manager.requestSerializer.setValue("iOS", forHTTPHeaderField:"deviceType")
    
        returnmanager
    
    }()
    
    typealiasrequestBack = (_response:Any?,_error:Error?) -> ()
    

    }

    extension HCNetWork {

    funcrequestDictionary(urlString:String, parameters: [String:AnyObject]?, finished:@escapingrequestBack) {
    
        letsuccess = { (task:URLSessionDataTask?, responseObject:Any?) -> ()in
    
            finished(responseObject,  nil)
    
        }
    
        letfailure = { (task:URLSessionDataTask?, error:Error) -> ()in
    
            finished(nil,  error)
    
        }
    
        post(urlString, parameters: parameters, progress:nil, success: success, failure: failure)
    
    } 
    

    }

    拿走可用

    相关文章

      网友评论

          本文标题:swift 使用 AFN 上传 json 参数

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