https://github.com/OpenFibers/OTHTTPRequest
項(xiàng)目中用到了埠居,使用簡(jiǎn)單查牌,還不錯(cuò),非常輕量級(jí)的下載庫(kù)滥壕,支持下載進(jìn)度與下載速度纸颜,預(yù)期大小與當(dāng)前下載大小。
下載
1)首先在下載的時(shí)候是存儲(chǔ)在temp文件夾下捏浊;
2)下載完成之后懂衩,會(huì)寫(xiě)入到Documents 下;
- (void)start {
if (_request) {
[_request pause];
_request = nil;
}
// 下載網(wǎng)址
NSString *downloadURLString = @"http://dl.google.com/drive/installgoogledrive.dmg";
// 保存路徑文件夾
NSString *documentPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
_request = [[OTHTTPDownloadRequest alloc] initWithURL:downloadURLString
cacheFile:[documentPath stringByAppendingPathComponent:@"temp"]
finishedFilePath:[documentPath stringByAppendingPathComponent:[downloadURLString lastPathComponent]]];
_request.delegate = self;
[_request start];
}
- (void)pause {
[_request pause];
}
// 下載信息等
- (void)downloadRequest:(OTHTTPDownloadRequest *)request
currentProgressUpdated:(float)progress
speed:(float)bytesPerSecond
totalReceived:(unsigned long long)totalReceived
expectedDataSize:(unsigned long long)expectedDataSize {
NSString *logInfo = [NSString stringWithFormat:
@"Download URL:\n%@\nprogress:%.2f %%\n\ndownloaded size:%.2fMB\n\nexpected size:%.2fMB\n\ncurrent speed:%.2f MB/s",
[request requestURL],
progress * 100,
[request downloadedFileSize] / (double) (1024 * 1024),
expectedDataSize / (double) (1024 * 1024),
request.averageDownloadSpeed / (double) (1024 * 1024)];
}
// 下載完成
- (void)downloadRequestFinished:(OTHTTPDownloadRequest *)request {
NSString *logInfo = [NSString stringWithFormat:
@"Download URL Finished:\n%@\n\nexpected size:%.2fMB",
[request requestURL],
[request expectedFileSize] / (double) (1024 * 1024)];
}
// 下載失敗
- (void)downloadRequestFailed:(OTHTTPDownloadRequest *)request error:(NSError *)error {
NSString *logInfo = [NSString stringWithFormat:
@"Download URL Failed:\n%@\n\n%@",
[request requestURL],
error];
}
// 文件保存失敗
-(void)downloadRequestWriteFileFailed:(OTHTTPDownloadRequest *)request exception:(NSException *)exception {
NSString *logInfo = [NSString stringWithFormat:
@"Write file failed:\n%@",
[request cacheFilePath]];
}
上傳
上傳,可以取消浊洞。
- (void)upload {
if (_request) {
[_request cancel];
_request = nil;
}
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Default" ofType:@"png"];
NSString *uploadURLString = @"upload";
_request = [[OTHTTPRequest alloc] initWithURL:[NSURL uploadURLString]];
[_request addPostValue:@"post_value" forKey:@"key"];
[_request addFileForKey:@"file" filePath:filePath fileName:@"Default.png" MIMEType:@"png"];
_request.delegate = self;
[_request start];
}
- (void)cancel {
[_request cancel];
}
// 上傳完成
- (void)otHTTPRequestFinished:(OTHTTPRequest *)request {
NSString *responseString = request.responseString;
_infoView.text = responseString;
}
// 上傳失敗
- (void)otHTTPRequestFailed:(OTHTTPRequest *)request error:(NSError *)error {
NSString *errorString = [NSString stringWithFormat:@"Request failed with error: %@", error];
}
// 上傳信息等
- (void)otHTTPRequest:(OTHTTPRequest *)request uploadProgressUpdated:(float)uploadProgress speed:(float)bytesPerSecond bytesSent:(unsigned long long)totalSent contentLength:(unsigned long long)contentLength {
NSString *logInfo = [NSString stringWithFormat:
@"Upload URL:\n%@\nprogress:%.2f %%\n\nuploaded size:%.2fKB\n\nexpected size:%.2fKB\n\ncurrent speed:%.2f KB/s",
[request URL],
uploadProgress * 100,
totalSent / (double) (1024),
contentLength / (double) (1024),
request.averageUploadSpeed / (double) (1024)];
}