iOS Development-File download (1 unreasonable)

Source: Internet
Author: User

iOS Development Web Chapter-File Download (one • unreasonable)

One, small file download

If the file is small, the download method will be more

Directly with NSData's + (ID) Datawithcontentsofurl: (nsurl *) URL;

Philip? Nsurlconnection send an HTTP request to download

If you are downloading pictures, you can also use the Sdwebimage framework

Second, sand box

1. In the Finder, some of the system's files (repositories) are hidden and can display hidden files by running code in the terminal.

Show hidden system files:

Defaults write Com.apple.finder appleshowallfiles YES

2. Create an alias to view the app sandbox.

The recommended tool, Simpholders, can be used to view the sandbox.

Three, large file download example

Hint: The project code, just for example, is not reasonable.

 1//2//YYVIEWCONTROLLER.M 3//01-File Download (unreasonable) 4//5//Created by Apple on 14-6-30. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYViewController.h" @interface Yyviewcontroller () @property (nonatomic,strong) Nsmutabledata *fil Edata;13-(ibaction) star;14 @end16 @implementation YYViewController18-(void) VIEWDIDLOAD20 {$ [Super viewd Idload];22}23-(ibaction) Star {25//Create download path Nsurl *url=[nsurl urlwithstring:@ "http://192.168.1.53:808 0/mjserver/resources/videosres.zip "];28 29//Create a request for Nsurlrequest *request=[nsurlrequest Requestwithurl:url]; 31 32//Send request (using proxy method) Nsurlconnection *connt=[nsurlconnection connectionwithrequest:request delegate:self];3 4 [connt start];35}36 PNs #pragma mark-nsurlconnectiondatadelegate Proxy method 38/*39 * When receiving a response from the server (connected to the server) will call the */41-(void ) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) Response42 {43//Initialize DATA44 Self.fileData=[nsmutabledata data];45}46 47/*48 * When data received from the server is called (may be called multiple times, only partial data is passed at a time) */50-(void) connection: ( Nsurlconnection *) connection didreceivedata: (NSData *) Data51 {52//1.1 points receive data. NSLog (@ "received data from the server! ---%d ", data.length); [Self.filedata appenddata:data];55}56 57/*58 * When the server's data is loaded, it will be called by the */60-(void) CONNECTIONDIDF Inishloading: (nsurlconnection *) connection61 {62//Download complete 63//Large file do not put documents, you can put library\caches or TMP64//NSSTR ing *fullpath=[caches stringbyappendingstring:@ "video.zip"];65 nsstring *caches = [Nssearchpathfordirectoriesin Domains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject];67 nsstring *filepath = [Caches stringbyappendingpathcom  ponent:@ "Video.zip"];68 [self.filedata writetofile:filepath atomically:yes];69 (@ "Download complete"); 71 NSLog}72 * Request error (failure) when called (Request timeout \ network \ No net \, generally referred to as client error) */75-(void) connection: (Nsurlconnection *) connection Didfailwitherror: ( Nserror *) error76 {}78 @end

Simulator:

Click on the screen to click on the download button to download the files on the local server.

Code Description:

In dealing with large file download problems, whether using synchronous or asynchronous download, is to get all the data at once, not advisable. Therefore, it is more appropriate to use the proxy way to handle.

In this project, it took a long time to splice the data, and it took a long time to put it on the write system sandbox. And it consumes a lot of memory space. This approach is unreasonable, and a more appropriate approach should be to stitch data while writing to the hard disk.

iOS Development-File download (1 unreasonable)

Related Article

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.