iOS Development Network programming--2, nsurlsessiondownloadtask file download

Source: Internet
Author: User
<span id="Label3"></p><p><p></p></p><p><p><span style="font-size: 18px;"><strong><span style="color: #ff0000;">This article outlines:</span></strong></span></p></p><p><p>1, Review Nsurlsessiontask</p></p><p><p>2, nsurlsessiondownloadtask large File block download</p></p><p><p>3, nsurlsessiondownloadtask Large File Agent method download</p></p><p><p><span style="font-size: 14px;">4, nsurlsessiondownloadtask Large file proxy method to achieve the breakpoint continued download</span></p></p><p><p></p></p><p><p>Preface: If the reader is the first time to read or to learn about this article to introduce nsurlsession knowledge, it is best to read my previous "ios Development network programming--1, nsurlsession Basic use" and then learn this article is Better.</p></p><p><p></p></p><p><p><strong><span style="font-size: 14pt;">1, Review Nsurlsessiontask:</span></strong></p></p><p><p>With nsurlsession you need to use nsurlsessiontask, so the relationship between the class of the task and its subclasses is still to be understood:</p></p><p><p>    </p></p><p><p>This article is about the large file download, since it is "download", so naturally to use is Nsurlsessiondownloadtask.</p></p><p><p></p></p><p><p><strong><span style="font-size: 14pt;">2, nsurlsessiondownloadtask large File block download</span></strong></p></p><p><p>Test API Interface: Http://120.25.226.186:32812/resources/images/minion_08.png (because of the disadvantages of block, so here with not hit the picture file as a Test)</p></p><p><p>2-1. description of class, method and Property:</p></p><p><p>Nsurlsession</p></p><p><p>+ (nsurlsession*) sharedsession; single-instance, directly create session sessions object</p></p><p><p>-(nsurlsessiondownloadtask*) downloadtaskwithurl:completionhandler:; Create a download task for the session based on the URL downloadtask, complete the callback block</p></p><p><p>Nsurlsessiondownloadtas</p></p><p><p>-(void) resume; Start and execute a task</p></p><p><p></p></p><p><p>2-2. Source Code display:</p></p><p><p></p></p><p><p>2-3. <span style="color: #ff0000;">Disadvantages</span> :</p></p><p><p>Block download method is not suitable for large file download, because this method needs to wait until the file download is complete, will be callback Completionhandler after the block parameter, and then in this block parameter can be</p></p><p><p>Gets the location (the path to the file download cache), response (response), Error Message.</p></p><p><p>In this case, for large files, we will not be able to get the download progress of the file in real-time during the download process.</p></p><p><p></p></p><p><p><span style="font-size: 14pt;"><strong>3, nsurlsessiondownloadtask Large File Agent method download</strong></span></p></p><p><p>Test API Interface: Http://120.25.226.186:32812/resources/videos/minion_02.mp4 (this time with a large file oh, but not too big, this basic exercise, Will not allow you to download it will take a few 10 minutes or more time??)</p></p><p><p>3-1. classes, methods, properties:</p></p><p><p>Nsurlsession</p></p><p><p>+ (nsurlsession*) sessionWithConfiguration:delegate:delegateQueue:; Creates a session Nsurlsession object based on the session configuration object and sets the execution queue for the agent and the agent</p></p><p><p>-(nsurlsessiondownloadtask*) downloadtaskwithurl; Task to create a session directly from a URL</p></p><p><p>Nsurlsessionconfiguration</p></p><p><p>+ (nsurlsessionconfiguration*) defaultsessionconfiguration; To create a default session configuration object</p></p><p><p>Nsurlsessiondownloadtask</p></p><p><p>-(void) resume to perform session tasks</p></p><p><p>Protocol and Proxy Methods:</p></p><p><p><NSURLSessionDownloadDelegate></p></p><p><p>    <span style="color: #3366ff;">-(void) Urlsession: (nsurlsession *) session downloadtask: (nsurlsessiondownloadtask *) Downloadtask</span><br><span style="color: #3366ff;">Didwritedata: (int64_t) byteswritten totalbyteswritten: (int64_t) totalbyteswritten totalbytesexpectedtowrite: (int64 _t) Totalbytesexpectedtowrite</span></p></p><p><p>Session Downloadtask Current session task Byteswritten This write data size Totalbyteswritten Write data size totalbytesexpectedtowrite total file size to download</p></p><p><p>    <span style="color: #3366ff;">-(void) Urlsession: (nsurlsession *) session downloadtask: (nsurlsessiondownloadtask *) Downloadtask</span><br><span style="color: #3366ff;">Didresumeatoffset: (int64_t) fileoffset expectedtotalbytes: (int64_t) expectedtotalbytes</span></p></p><p><p>Didresumeatoffset the proxy method that is called when the download is resumed</p></p><p><p>    <span style="color: #3366ff;">-(void) Urlsession: (nsurlsession *) session downloadtask: (nsurlsessiondownloadtask *) Downloadtask</span><br><span style="color: #3366ff;">Didfinishdownloadingtourl: (nsurl *) Location</span></p></p><p><p>Didfinishdownloadingtourl: (nsurl *) Location Internal has implemented the side download side writes the data to the sandbox the operation</p></p><p><p>Downloadtask can get the response header information when inside</p></p><p><p><span style="color: #3366ff;">-(void) Urlsession: (nsurlsession *) session task: (nsurlsessiontask *) task didcompletewitherror: (nserror *) Error</span></p></p><p><p>Request failure returns the proxy method called by the error message</p></p><p><p>3-2. Source code Display</p></p><p><p>    </p></p><p><p>3-3, compared to block download method advantages:</p></p><p><p>You can get the download progress in real Time.</p></p><p><p>3-4. Source Code Download:</p></p><p><p>Baidu Cloud Share link: http://pan.baidu.com/s/1i4jmNIl password: M5JK</p></p><p><p>3-5, can see the effect:</p></p><p><p>      </p></p><p><p>Then under this path you can find the downloaded MP4 file.</p></p><p><p></p></p><p><p><span style="font-size: 14pt;"><strong>4, nsurlsessiondownloadtask Large file proxy method to achieve the breakpoint continued download</strong></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;">The following example is further described on the basis of the proxy method download for the 3, nsurlsessiondownloadtask large File.<br></span></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;">The API for using the test is still the same: Http://120.25.226.186:32812/resources/videos/minion_02.mp4</span></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;">4-1, in order to better implement the breakpoint continuation, here need to add three button implementation: start download, pause the download, resume the download, and associated to the Code.</span></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;">     </span></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;">4-2. the classes and methods involved in the continuation of the breakpoint</span></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;">Nsurlsessiondownloadtask:</span></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;">-(void) suspend; paused, can be resumed<br></span></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;"><span style="font-size: 14pt;"><span style="font-size: 14px;">-(void) cancel; cancel, can not restore<br></span></span></span></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;">-(void) cancelbyproducingresumedata:^ (nsdata * _nullable resumedata):; Cancellation of<br></span></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;"><span style="font-size: 14pt;"><span style="font-size: 14px;">-(void) resume; Resume under Create a new task, equivalent to restarting a task<br></span></span></span></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;"><span style="font-size: 14pt;"><span style="font-size: 14px;">4-3, Use the above method note</span></span></span></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;"><span style="font-size: 14pt;"><span style="font-size: 14px;">4-3-1, If you use the Suspend method to pause the download, because it is recoverable, the corresponding download task object is Unique.</span></span></span></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;"><span style="font-size: 14pt;"><span style="font-size: 14px;">        </span></span></span></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;"><span style="font-size: 14pt;"><span style="font-size: 14px;">When used, suspend and resume are used as object methods that are called by the same nsurlsessiondownloadtask.</span></span></span></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;"><span style="font-size: 14pt;"><span style="font-size: 14px;">    <span style="font-size: 14pt;"><span style="font-size: 14px;"><span style="font-size: 14pt;"><span style="font-size: 14px;">4-3-2,</span></span></span></span> If you use cancel, is equivalent to Nsurlsessiondwonloadtask task is also canceled. So if you re-download it, you need to recreate the <span style="font-size: 14pt;"><span style="font-size: 14px;"><span style="font-size: 14pt;"><span style="font-size: 14px;">nsurlsessiondownloadtask</span></span></span></span> object.</span></span></span></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;"><span style="font-size: 14pt;"><span style="font-size: 14px;">At the same time, you can no longer start the download again, it should be heavy before the end of the downloaded section to start the Download.</span></span></span></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;"><span style="font-size: 14pt;"><span style="font-size: 14px;">The following changes the UIButton display string to "cancel"</span></span></span></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;"><span style="font-size: 14pt;"><span style="font-size: 14px;">      </span></span></span></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;"><span style="font-size: 14pt;"><span style="font-size: 14px;">If you cannot resume the download using cancel, you can only use the <span style="font-size: 14pt;"><span style="font-size: 14px;">cancelbyproducingresumedata:^ (nsdata * _nullable Resumedata)</span></span> method in order to resume the Download.</span></span></span></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;"><span style="font-size: 14pt;"><span style="font-size: 14px;">The Resumedata in this method stores information about the data that was previously downloaded: the file name, storage location, The length of the downloaded data, and so on, not the downloaded data itself.</span></span></span></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;"><span style="font-size: 14pt;"><span style="font-size: 14px;">Resuming the download is also required through this <span style="font-size: 14pt;"><span style="font-size: 14px;"><span style="font-size: 14pt;"><span style="font-size: 14px;">resumedata</span></span></span></span> to recover, and then continue to Download. Also re-create the download task object Nsurlsessiondownloadtask.</span></span></span></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;"><span style="font-size: 14pt;"><span style="font-size: 14px;">        </span></span></span></span></p></p><p><p><span style="font-size: 14pt;"><span style="font-size: 14px;"><span style="font-size: 14pt;"><span style="font-size: 14px;">       </span></span></span></span></p></p><p><p>In this method, there will be a bug, if the user then click on the "continue to download" button, create a new task, a new task will also be new THREADS.</p></p><p><p>4-4. Source Code Download:</p></p><p><p>Baidu Cloud Share link: Http://pan.baidu.com/s/1qXnIsYK password: ekx2</p></p><p><p>4-5, unable to deal with the Needs:</p></p><p><p>For exiting the application, and then restarting the program, it is not possible to continue downloading the breakpoint and can only be downloaded Again.</p></p><p><p>first, seemingly feasible ideas: can be stored locally, but the premise of storage is that the user clicked the "cancel download" button, The actual situation is often the user directly quit the application and generally do not go to click "cancel button" in advance, resumdata</p></p><p><p>If this is the case, the resumedata is not stored locally and will not be able to restart the application before continuing with the Download.</p></p><p><p>     </p></p><p><p>Reprint annotated Source: http://www.cnblogs.com/goodboy-heyang/p/5195806.html, respect for the fruits of Labor.</p></p><p><p></p></p><p><p>iOS Development Network programming--2, nsurlsessiondownloadtask file download</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.