iOS 檔案下載,ios檔案下載
iOS 視頻音樂類等應用會用到“檔案下載”。檔案下載在iOS中的實現如下:
1、小檔案下載
@interface ViewController () <NSURLConnectionDataDelegate>@property (weak, nonatomic) IBOutlet UIProgressView *progressView;/** 檔案資料 */@property (nonatomic, strong) NSMutableData *fileData;/** 檔案的總長度 */@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 - <NSURLConnectionDataDelegate>- (void)connection:(NSURLConnection *)connection didReceiveResponse:(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(@"已下載:%.2f%%", (progress) * 100); self.progressView.progress = progress;}- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ NSLog(@"下載完畢"); // 將檔案寫入沙箱中 // 快取檔案夾 NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; // 檔案路徑 NSString *file = [caches stringByAppendingPathComponent:@"minion_15.mp4"]; // 寫入資料 [self.fileData writeToFile:file atomically:YES]; self.fileData = nil;}
2、大檔案下載
#define XMGFile [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"minion_15.mp4"]@interface ViewController () <NSURLConnectionDataDelegate>@property (weak, nonatomic) IBOutlet UIProgressView *progressView;/** 檔案的總長度 */@property (nonatomic, assign) NSInteger contentLength;/** 當前下載的總長度 */@property (nonatomic, assign) NSInteger currentLength;/** 檔案控制代碼對象 */@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>/** * 接收到響應的時候:建立一個空的檔案 */- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response{ // 獲得檔案的總長度 self.contentLength = [response.allHeaderFields[@"Content-Length"] integerValue]; // 建立一個空的檔案 [[NSFileManager defaultManager] createFileAtPath:XMGFile contents:nil attributes:nil]; // 建立檔案控制代碼 self.handle = [NSFileHandle fileHandleForWritingAtPath:XMGFile];}/** * 接收到具體資料:馬上把資料寫入一開始建立好的檔案 */- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ // 指定資料的寫入位置 -- 檔案內容的最後面 [self.handle seekToEndOfFile]; // 寫入資料 [self.handle writeData:data]; // 拼接總長度 self.currentLength += data.length; // 進度 self.progressView.progress = 1.0 * self.currentLength / self.contentLength;}- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ // 關閉handle [self.handle closeFile]; self.handle = nil; // 清空長度 self.currentLength = 0;}