我正在使用以下代码从 url 下载 epub /pdf。我喜欢提供一个进度条,所以当我开始下载时它会显示进度,下载完成时会弹出一条消息。我该如何实施?
我的下载文件代码
-(void)Download
{
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.feedbooks.com/book/3471.epub"]];
//Store the Data locally as epub File if u want pdf change the file extension
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"3471.epub"];
[pdfData writeToFile:filePath atomically:YES];
NSLog(@"%@",filePath);
}
我在我的 .m 文件中使用此代码,但它不适合我
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
_totalFileSize = response.expectedContentLength;
responseData = [[NSMutableData alloc] init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
_receivedDataBytes += [data length];
MyProgressBar.progress = _receivedDataBytes / (float)_totalFileSize;
[responseData appendData:data];
}