#import "MainViewController.h" @interface Mainviewcontroller () @end @implementation mainviewcontroller-(void) viewdidload{[Super Viewdidload]; Download file [self download];} -(void) download{//1. Nsurl Nsurl *url = [Nsurl urlwithstring:@ "Http://loacal/~apple/itcase/download/iTunesConnect_DeveloperGuide_CN.zip" ]; 2NSRequest//To determine the size of a file on a network server, you can use the Head method of http Nsmutableurlrequest *request = [Nsmutableurlrequest requestwithurl:url ]; Using the head method, only the information of the target file is obtained, not the actual download work. [Request sethttpmethod:@ "HEAD"]; /** Set Breakpoints: Headerfield: Header Domain (field of the request header) you can incrementally download the data within the specified range by specifying a range, and then stitching the data into one file after the download is complete. 1 According to the head method to get to the total size of the file to be downloaded, 2 to create a temporary buffer file on the disk, the size of the file and the target file size consistent 3 buffer file all the bytes are default to 0 4 open multi-threaded, respectively load different range header specified data block, the data block loading is completed to , it is written to the corresponding offset address respectively. 5 After all the data has been downloaded, the file download is complete, and the temporary filename is changed to the destination file. Development difficulties: 0 before writing a file, you must first create a file of the same size. 1 file read and write problems, in OC default is overwrite, append, if you want to specify the location, need to use the Seek method, move the file pointer. 2 Locking the file is an issue when writing to a file in multiple threads. */[Request setvalue:@ "bytes=0-499" ForkeThe ypath:@ "range"];//indicates that the data is read only from the No. 0 byte to the No. 499 byte. 3NSURLConnection//If you want to get the file length, you can get nsurlresponse *response = nil in response; NSData *data = [nsurlconnection sendsynchronousrequest:request returningresponse:&response Error:nil]; In the Expectedcontentlength property of response, you can learn the file length of the file you want to unload. NSLog (@ "%lld%d%@", [response expectedcontentlength], data.length, data); } @end