美文网首页iOS开发
Moya 如何自定义 超时、 HTTP Header 等

Moya 如何自定义 超时、 HTTP Header 等

作者: 波波熊洛夫 | 来源:发表于2017-09-02 11:22 被阅读115次
    import Alamofire
    import Moya
    
    class WebService {
        
        // set false when release
        static var verbose: Bool = true
        
        // session manager
        static func manager() -> Alamofire.SessionManager {
            let configuration = URLSessionConfiguration.default
            configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
            configuration.timeoutIntervalForRequest = 15 // timeout
            let manager = Alamofire.SessionManager(configuration: configuration)
            manager.adapter = CustomRequestAdapter()
            return manager
        }
        
        // request adpater to add default http header parameter
        private class CustomRequestAdapter: RequestAdapter {
            public func adapt(_ urlRequest: URLRequest) throws -> URLRequest {
                var urlRequest = urlRequest
                urlRequest.setValue("XMLHttpRequest", forHTTPHeaderField: "X-Requested-With")          
                return urlRequest
            }
        }
        
        // response result type
        enum Result {
            case success(JSON)
            case failure(String)
        }
    }
    
    // use WebService.manager for provider
    static let provider = MoyaProvider<TargetType>(manager: WebService.manager())
    

    相关文章

      网友评论

        本文标题:Moya 如何自定义 超时、 HTTP Header 等

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