DownloadKit
示例
要运行示例项目,请克隆仓库,然后首先从示例目录运行 `pod install`。
或者运行 `pod try DownloadKit`
要求
- iOS 10.0+
- Xcode 10.0+
- Swift 4.2+
安装
DownloadKit 可通过 CocoaPods 获取。安装它,只需将以下行添加到您的 Podfile 即可:
pod 'DownloadKit'
用法
共享实例
下载工具(DownloadKit)的共享实例可用于现有的下载请求管理。
import DownloadKit
let downloader = DownloadKit.shared
共享实例默认的内存缓存大小为150MB。
下载图片
可以从指定的URL
下载图片,库会解析下载的数据为一个UIImage
对象并从中返回,如果解析失败则返回 nil。
let url = URL(string: "https://httpbin.org/image/jpeg")
DownloadKit.shared.requestImage(from: url, completion: { (image, error) in
guard error == nil else {
print("Error Downloading:", error!)
return
}
if let image = image {
print("Downloaded Image:", image)
}
})
下载JSON
可以从指定的URL
下载JSON对象,库将返回的数据解析到提供的数据模型中。提供的数据模型必须符合Codable
协议。例如,以下JSON对象必须被下载
{
"userId": 1,
"id": 1,
"title": "JSON Example",
"body": "This is how you download a JSON file."
}
为上述JSON对象创建一个符合Codable
协议的数据模型
struct Post: Codable {
let userId: Int
let id: Int
let title: String
let body: String
}
最后,调用requestJSON(from:model:completion)
,传入你的JSON数据模型。
let url = URL(string: "https://jsonplaceholder.typicode.com/posts")
DownloadKit.shared.requestImage(from: url, model: [posts].self, completion: { (posts, error) in
guard error == nil else {
print("Error Downloading:", error!)
return
}
if let posts = posts {
print("Downloaded JSON:", posts)
}
})
下载原始数据
该库还允许从指定的URL
下载任何其他文件。下载的数据以Data
对象返回,您可以按需解析该数据或存储。
let url = URL(string: "https://httpbin.org/image/jpeg")
DownloadKit.shared.request(from: url, completion: { (data, error) in
guard error == nil else {
print("Error Downloading:", error!)
return
}
if let data = data {
print("Downloaded Data:", data)
}
})
取消请求
可以通过调用cancelRequest(reference:)
取消任何下载请求。 Whenever a request is made a RequestReference
object is returned, which can be utilised to cancel the request.
let url = URL(string: "https://httpbin.org/image/jpeg")
let ref = DownloadKit.shared.request(from: url, completion: { (data, error) in
guard error == nil else {
print("Error Downloading:", error!)
return
}
if let data = data {
print("Downloaded Data:", data)
}
})
DownloadKit.shared.cancelRequest(reference: ref)
请求被取消后,请求的完成块将通过一个DownloadKitError
传递,指示请求被用户取消。
缓存
该库内置了内存缓存。这是通过底层使用URLCache
实现的,该配置将下载的数据缓存到内存中。因此,对同一端点的多个请求将返回缓存的结果,这有助于确保效率。
配置缓存大小
可以根据需要配置缓存大小。然而,确保你对自己的实现所需的缓存大小做出明智的决策。建议在应用启动时更改DownloadKit共享实例的缓存大小。但是对于新的DownloadKit实例,缓存将独立于共享实例。缓存将按需自动驱逐文件。
默认缓存大小设置为150 MB。
在你的AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
DownloadKit.shared = DownloadKit(memorySize: 150 * 1024 * 1024) // 150 MB cache
return true
}
由于Apple对
URLCache
的实现方式,只有当文件大小小于总缓存大小的5%时,才会将下载的文件存储到缓存中。因此在选择缓存大小时请考虑这一点。
清除缓存
如果需要新鲜副本的文件,可以手动清除缓存。你可以使用clearCache()
清除整个缓存。
DownloadKit.shared.clearCache()
或者,你可以使用clearCache(for:)
从缓存中清除特定文件。
let url = URL(string: "https://httpbin.org/image/jpeg")
DownloadKit.shared.clearCache(for: url)
清除特定URL
的缓存后,对该URL的任何新请求都将检索新数据。
作者
moizullah, [email protected]
许可协议
DownloadKit 在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。