網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
iOS使用NSURLConnection實(shí)現(xiàn)斷點(diǎn)續(xù)傳下載_IOS
作者:夕陽(yáng)下的守望者 ? 更新時(shí)間: 2022-06-25 編程語(yǔ)言本文實(shí)例為大家分享了iOS使用NSURLConnection實(shí)現(xiàn)斷點(diǎn)續(xù)傳下載的具體代碼,供大家參考,具體內(nèi)容如下
一.斷點(diǎn)續(xù)傳的原理
斷點(diǎn)續(xù)傳的原理:每次在想服務(wù)器請(qǐng)求下載數(shù)據(jù)的同時(shí),要告訴服務(wù)器從整個(gè)下載文件的數(shù)據(jù)流的某個(gè)還未下載的位置開始下載,然后服務(wù)器就返回從哪個(gè)位置開始的數(shù)據(jù)流
二.斷點(diǎn)續(xù)傳的實(shí)現(xiàn)
第一步:先聲明一些屬性
fileprivate var totalSize: Int64 = 0 ? // 總大小
fileprivate var currentSize: Int64 = 0 // 當(dāng)前大小
fileprivate var fileName: String? ? ? ?// 文件名
fileprivate var fullPath: String? ? ? ?// 文件路勁
fileprivate var handle: FileHandle? ? ?// 句柄
fileprivate var connection: NSURLConnection?
第二步:創(chuàng)建URL和請(qǐng)求
關(guān)鍵是設(shè)置請(qǐng)求頭
// 下載文件
func urlConnectionDownload(_ url: String) -> NSURLConnection? {
? ? ? ? var request = URLRequest(url: URL(string: url)!)
? ? ? ? // 設(shè)置請(qǐng)求頭信息
? ? ? ? /*
? ? ? ? ?bytes=0-1000 表示下載0-1000的數(shù)據(jù)
? ? ? ? ?bytes=0- ? ? 表示從0開始下載直到下載完畢
? ? ? ? ?bytes=100- ? 表示從100開始下載直到下載完畢
? ? ? ? ?*/
? ? ? ? request.setValue("bytes=\(currentSize)", forHTTPHeaderField: "Range")
? ? ? ? // 發(fā)送異步請(qǐng)求
? ? ? ? connection = NSURLConnection(request: request, delegate: self)
? ? ? ? return connection
? ? }
? ? // 取消下載文件
? ? func urlConnectionCacel() {
? ? ? ? connection?.cancel()
? ? }
第三步:設(shè)置代理NSURLConnectionDataDelegate
第四步:實(shí)現(xiàn)代理NSURLConnectionDataDelegate方法
// 接收到響應(yīng)頭信息的時(shí)候就會(huì)調(diào)用(最先調(diào)用的方法),只會(huì)調(diào)用一次
? ? func connection(_ connection: NSURLConnection, didReceive response: URLResponse) {
? ? ? ? print("didReceive response")
? ? ? ? // 判斷是否已經(jīng)下載過(guò)了
? ? ? ? if currentSize > 0 {
? ? ? ? ? ? // 已經(jīng)下載過(guò)的話,就不需要再次接受response了
? ? ? ? ? ? return
? ? ? ? }
? ? ? ? // 文件的總大小
? ? ? ? totalSize = response.expectedContentLength
? ? ? ? // 得到的文件名稱
? ? ? ? fileName = response.suggestedFilename
? ? ? ? // 邊接收數(shù)據(jù)邊寫文件到沙盒中
? ? ? ? // 1. 獲取文件的全路徑
? ? ? ? if let cache = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).last {
? ? ? ? ? ? let nsCache = cache as NSString
? ? ? ? ? ? fullPath = nsCache.appendingPathComponent(fileName!)
? ? ? ? ? ? // 創(chuàng)建一個(gè)空的文件
? ? ? ? ? ? FileManager.default.createFile(atPath: fullPath!, contents: nil, attributes: nil)
? ? ? ? ? ? // 創(chuàng)建句柄
? ? ? ? ? ? handle = FileHandle(forWritingAtPath: fullPath!)
? ? ? ? }
? ? }
? ??
? ? func connection(_ connection: NSURLConnection, didReceive data: Data) {
? ? ? ? print("didReceive data")
? ? ? ? // 把文件句柄移動(dòng)到文件的末尾
? ? ? ? handle?.seekToEndOfFile()
? ? ? ? // 使用文件句柄寫數(shù)據(jù)
? ? ? ? handle?.write(data)
? ? ? ? currentSize += data.count
? ? ? ? print(currentSize / totalSize)
? ? }
? ??
? ? func connectionDidFinishLoading(_ connection: NSURLConnection) {
? ? ? ? print("didFinish loading")
? ? ? ? print(fullPath!)
? ? ? ? handle?.closeFile()
? ? ? ? handle = nil
? ? }
原文鏈接:https://blog.csdn.net/wgl_happy/article/details/75610272
相關(guān)推薦
- 2022-10-26 jQuery中DOM?屬性使用實(shí)例詳解下篇_jquery
- 2022-12-10 Qt中簡(jiǎn)單的按鈕槽函數(shù)傳遞參數(shù)方法_C 語(yǔ)言
- 2022-09-25 Redis時(shí)單線程設(shè)計(jì)的,為什么還這么快
- 2022-07-12 tk mybatis報(bào)錯(cuò):org.apache.ibatis.binding.BindingExce
- 2022-04-21 詳解Golang?Map中的key為什么是無(wú)序的_Golang
- 2023-03-15 Python多進(jìn)程協(xié)作模擬實(shí)現(xiàn)流程_python
- 2022-11-10 Android自定義DataTimePicker日期時(shí)間選擇器使用詳解_Android
- 2022-01-18 解決CSRF verification failed. Request aborted.的問(wèn)題
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支