SessionManager
描述
- 这是一个轻量级的网络库,可以用来向服务器发送 http 请求。
- 此库使用 Apple 的 URLSession 框架来处理网络调用。
如果您喜欢 SessionManager,别忘了在页面右上角给一个星标。我们欢迎任何建议/贡献。
特性
- 支持处理所有类型的错误代码的 http 调用。
- 集成 Cocoapods。
- 实现了错误处理。
- 支持不同 API 下载和上传文件。
需求
- iOS 10.0+
- Xcode 10+
安装
将以下行添加到您的 Podfile 中
pod 'SessionManager', '~> 1.0.4'
- SessionManager 在CocoaPods上可用。
- 当前版本与 Swift 4.1 兼容。
使用
多部分 API 请求
创建用于图片/pdf 文件的文件对象。
方法: 1
File(name: documentName, type: MimeType, fileData: Data)
#name - 对于后端 API 所需的文档名称。#type: 文件的 MIME 类型。有关 MIME 类型的更多信息,请参考库中的 MimeType 枚举。#fileData: 接受文件的 Data。
方法: 2
File(name: documentName, type: MimeType, filePath: filePath)
#name - 对于后端 API 所需的文档名称。#type: 文件的 MIME 类型。有关 MIME 类型的更多信息,请参考库中的 MimeType 枚举。#filePath: 需要iPhone上保存的文件的路径。
多部分 API 请求
创建请求
let parameters: [String: Any] = ["demo": "done"]
let file: File = File(name: documentName, type: MimeType, fileData: Data)
// Turning on activity indicator.
UIApplication.shared.isNetworkActivityIndicatorVisible = true
do {
try SessionManager(taskType: .data, config: .default).handleMultipart(withRequestUrl: url, httpMethod: .post, params: params, files: [file], completion: { (data, error) in
DispatchQueue.main.async {
// Turning off activity indicator.
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
if let networkError = error {
switch networkError.errorCode {
case .validationError:
break
case .noInternetConnection:
break
default:
DispatchQueue.main.async {
self.showAlert(message: networkError.error)
}
return
}
}
// No Error Found.
do {
let json = try JSONSerialization.jsonObject(with: response, options: JSONSerialization.ReadingOptions.mutableContainers)
print(json)
} catch let error {
// Handle Error
print(error)
}
})
} catch HTTPError.handler {
print(HTTPError.handler.failureReason ?? "")
} catch HTTPError.multipartHandler {
print(HTTPError.multipartHandler.failureReason ?? "")
} catch HTTPError.urlCreationFailure {
print(HTTPError.urlCreationFailure.failureReason ?? "")
} catch HTTPError.urlEncodingFailed {
print(HTTPError.urlEncodingFailed.failureReason ?? "")
} catch let error {
print(error.localizedDescription)
}
正常API请求
创建请求
let parameters: [String: Any] = ["demo": "done"]
do {
try SessionManager(taskType: .data, config: .default).config(timeout: 5.0).handle(withRequestUrl: url, httpMethod: .get, params: nil, completion: { (data, error) in
DispatchQueue.main.async {
// Turning off activity indicator.
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
if let networkError = error {
switch networkError.errorCode {
case .validationError:
break
case .noInternetConnection:
break
default:
DispatchQueue.main.async {
self.showAlert(message: networkError.error)
}
return
}
}
// No error found
do {
let json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)
print(json)
} catch let error {
// Handle Error
print(error)
}
})
// Handle Error from Api
} catch HTTPError.handler {
print(HTTPError.handler.failureReason ?? "")
} catch HTTPError.urlCreationFailure {
print(HTTPError.urlCreationFailure.failureReason ?? "")
} catch HTTPError.urlEncodingFailed {
print(HTTPError.urlEncodingFailed.failureReason ?? "")
} catch let error {
print(error)
}
贡献
任何有助于让其成为更好库的贡献都受到欢迎。
作者
许可
本项目遵循MIT许可协议 - LICENSE。