Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use NSURLConnection in iOS to download download from breakpoint

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how iOS uses NSURLConnection to continue downloading at breakpoints". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

one。 The principle of breakpoint continuation

The principle of breakpoint continuation: every time you ask the server to download data, tell the server to start downloading from some undownloaded location of the entire data stream of the downloaded file, and then the server returns the data stream from which location

two。 The realization of breakpoint continuation

Step 1: declare some properties first

Fileprivate var totalSize: Int64 = 0 / / Total size fileprivate var currentSize: Int64 = 0 / / current size fileprivate var fileName: String? / / File name fileprivate var fullPath: String? / / File Road strength fileprivate var handle: FileHandle? / / handle fileprivate var connection: NSURLConnection?

Step 2: create URL and request

The key is to set the request header

/ / download file func urlConnectionDownload (_ url: String)-> NSURLConnection? {var request = URLRequest (url: URL (string: url)!) / / set request header information / * bytes=0- 1000 means to download the data from 0 to 1000 bytes=0- means to download from 0 to the end of the download bytes=100- means to download from 1000 to the following Finished loading * / request.setValue ("bytes=\ (currentSize)" ForHTTPHeaderField: "Range") / / send an asynchronous request connection = NSURLConnection (request: request, delegate: self) return connection} / / cancel the download file func urlConnectionCacel () {connection?.cancel ()}

Step 3: set up proxy NSURLConnectionDataDelegate

Step 4: implement the proxy NSURLConnectionDataDelegate method

/ / it will be called (the method called first) when the response header information is received, and func connection (_ connection: NSURLConnection, didReceive response: URLResponse) {print ("didReceive response") / / will be called only once to determine whether if currentSize > 0 {/ / has been downloaded. There is no need to accept response again the total size of the return} / / file totalSize = response.expectedContentLength / / the file name fileName = response.suggestedFilename / / write the file to sandboxie / / 1 while receiving data. Get the full path of the file if let cache = NSSearchPathForDirectoriesInDomains (.cachesDirectory, .userDomainMask, true). Last {let nsCache = cache as NSString fullPath = nsCache.appendingPathComponent (fileName!) / / create an empty file FileManager.default.createFile (atPath: fullPathology, contents: nil) Attributes: nil) / / create handle handle = FileHandle (forWritingAtPath: fullPath!)}} func connection (_ connection: NSURLConnection) DidReceive data: Data) {print ("didReceive data") / / move the file handle to the end of the file handle?.seekToEndOfFile () / / write data handle?.write (data) currentSize + = data.count print (currentSize / totalSize)} func connectionDidFinishLoading (_ connection: NSURLConnection) {print ("didFinish loading") print (fullPath!) Handle?.closeFile () handle = nil} "how to use NSURLConnection to implement breakpoint download in iOS" ends here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report