IOS file downloads and ios file downloads
Apps such as iOS video and music will use "File Download ". The implementation of File Download in iOS is as follows:
1. Download small files
@ Interface ViewController () <NSURLConnectionDataDelegate> @ property (weak, nonatomic) IBOutlet UIProgressView * progressView;/** file data */@ property (nonatomic, strong) NSMutableData * fileData; /** total file length */@ property (nonatomic, assign) NSInteger contentLength; @ end
-(Void) viewDidLoad {[super viewDidLoad]; NSURL * url = [NSURL URLWithString: @ "http: // 120.25.226.186: 32812/resources/videos/minion_15.mp4"]; [NSURLConnection connectionWithRequest: [NSURLRequest requestWithURL: url] delegate: self] ;}# pragma mark-<signature>-(void) connection :( NSURLConnection *) connection response :( NSHTTPURLResponse *) response {self. contentLength = [response. allHeaderFields [@ "Content-Length"] integerValue]; self. fileData = [NSMutableData data];}-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {[self. fileData appendData: data]; CGFloat progress = 1.0 * self. fileData. length/self. contentLength; NSLog (@ "downloaded: %. 2f % ", (progress) * 100); self. progressView. progress = progress;}-(void) connectionDidFinishLoading :( NSURLConnection *) connection {NSLog (@ "download completed "); // write the file to the sandbox. // The cache folder NSString * caches = [Export (NSCachesDirectory, NSUserDomainMask, YES) lastObject]; // the file path NSString * file = [caches stringByAppendingPathComponent: @ "minion_15.mp4"]; // write data to [self. fileData writeToFile: file atomically: YES]; self. fileData = nil ;}
2. Download large files
# Define XMGFile [[callback (NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: @ "minion_15.mp4"] @ interface ViewController () <strong> @ property (weak, nonatomic) IBOutlet UIProgressView * progressView;/** total file length */@ property (nonatomic, assign) NSInteger contentLength;/** total downloaded length */@ property (nonatomic, assign) NSInteger currentLength;/** file handle object */@ property (nonatomic, strong) NSFileHandle * handle; @ end
- (void)viewDidLoad { [super viewDidLoad]; NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_15.mp4"]; [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url] delegate:self];}
# Pragma mark-<NSURLConnectionDataDelegate>/*** when a response is received: Create an empty file */-(void) connection :( NSURLConnection *) connection didReceiveResponse :( NSHTTPURLResponse *) response {// get the total length of the file self. contentLength = [response. allHeaderFields [@ "Content-Length"] integerValue]; // create an empty file [[NSFileManager defaultManager] createFileAtPath: XMGFile contents: nil attributes: nil]; // create a file handle self. handle = [NSFileHandle fileHandleForWritingAtPath: XMGFile];}/*** receives specific data: Write the data to the file created at the beginning immediately */-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {// specify the data write location -- the last part of the file content [self. handle seekToEndOfFile]; // write data [self. handle writeData: data]; // total stitching length self. currentLength + = data. length; // progress self. progressView. progress = 1.0 * self. currentLength/self. contentLength;}-(void) connectionDidFinishLoading :( NSURLConnection *) connection {// close handle [self. handle closeFile]; self. handle = nil; // clear the length self. currentLength = 0 ;}