日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(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

欄目分類
最近更新