Skip to content

ALNetWorkingSwift is a swift network request library that integrates Alamofire and HandyJSON.

License

Notifications You must be signed in to change notification settings

Anyeler/ALNetWorkingSwift

Repository files navigation

ALNetWorkingSwift

ALNetWorkingSwift is mainly provided for internal Swift projects within the company. So far, network request-related operations have been integrated, including the data mapping module.

CI Status Version License Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • iOS 8.0+
  • Xcode 9.3+
  • Swift 4.1+

Installation

ALNetWorkingSwift is available through CocoaPods. To install it, simply add the following line to your Podfile:

platform :ios, '8.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'ALNetWorkingSwift'
    # You can also load only core modules:
    # pod 'ALNetWorkingSwift/Core'
end

Basic usage of core modules

Making request

You can invoke the method to initiate a common network request:

ALHTTPRequestOperationManager.default.requestBase(httpMethod: .get, url: "https://www.baidu.com", urlEncoding: TURLEncoding.default, parameter: nil) { (response) in        
    switch response.result {
    case .success(let res):
        print(res)
    case .failure(let err):
        print(err)
    }
}

You can also call the following methods to upload data:

ALHTTPRequestOperationManager.default.uploadBase(url: "https://www.baidu.com", multipartFormData: { (formData) in
    // The assembly to upload data
}) { (result) in
    switch result {
    case .success(let request, let streamingFromDisk, let streamFileURL):
        print(request)
        print(streamingFromDisk)
        print(streamFileURL ?? "")
    case .failure(let err):
        print(err)
    }
}

Advanced usage

You can also re-encapsulate both approaches to meet your business requirements.

Configuration

To conform to ALCommonConfigProtocol, a struct need to implement Some properties and methods:

public struct HTTPConfig: ALCommonConfigProtocol {
    
    public var kHttpUserAgent: String = ""
    
    init() {
        
    }
    
    public func getHeader(dictHeader: [String: String]? = nil) -> [String: String] {
        var header: [String:String] = [String: String]()
        if dictHeader != nil {
            header.merge(dictHeader!) { (_, new) in new }
        }
        return header
    }
    
    public func getContentType(contentType: Set<String>? = nil) -> Set<String> {
        var content: Set<String> = Set<String>()
        contentType?.forEach({ (ele) in
            content.insert(ele)
        })
        return content
    }
}

Then, Call this method:

ALHTTPRequestOperationManager.default.httpConfig = HTTPConfig()

Author

Anyeler, 414116969@qq.com

License

ALNetWorkingSwift is available under the MIT license. See the LICENSE file for more info.

About

ALNetWorkingSwift is a swift network request library that integrates Alamofire and HandyJSON.

Resources

License

Stars

Watchers

Forks

Packages

No packages published