#import "ViewController.h"
#import "ProgressButton.h"
@interface ViewController ()<NSURLConnectionDataDelegate>
@property (nonatomic, assign) long long fileSize;
@property (nonatomic, assign) long long currentLocalSize;
@property (nonatomic, assign) long long currentSize;
@property (nonatomic, strong) NSOutputStream *output;
@property (nonatomic, strong) NSURLConnection *connection;
@property (nonatomic, copy) NSString *filePath;
@property (weak, nonatomic) IBOutlet ProgressButton *progressBtn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)pause:(id)sender {
[self.connection cancel];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self serverFileSize];
}
- (void)serverFileSize {
NSString *URLStr =@"http://localhost/01UI基础复习.mp4";
URLStr = [URLStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:URLStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"HEAD"];
NSHTTPURLResponse *response;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];
self.filePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:response.suggestedFilename];
self.fileSize = response.expectedContentLength;
NSLog(@"%@",self.filePath);
self.currentLocalSize = [self checkLocalFile];
if (self.currentLocalSize == self.fileSize) {
NSLog(@"下载成功");
return;
}
[self download:url];
}
- (long long)checkLocalFile {
long long fileSize = 0;
NSFileManager *manager = [NSFileManager defaultManager];
NSDictionary *att = [manager attributesOfItemAtPath:self.filePath error:NULL];
long long localFileSize = att.fileSize;
if (localFileSize > self.fileSize) {
[manager removeItemAtPath:self.filePath error:NULL];
fileSize = 0;
}else if (localFileSize == self.fileSize) {
fileSize = localFileSize;
}else {
fileSize = localFileSize;
}
return fileSize;
}
- (void)download:(NSURL *)url {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *range = [NSString stringWithFormat:@"bytes=%lld-",self.currentLocalSize];
[request setValue:range forHTTPHeaderField:@"Range"];
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
[[NSRunLoop currentRunLoop] run];
});
}
#pragma mark - NSURLConnection 代理
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"接收到响应%@ -- %lld",response,response.expectedContentLength);
self.output = [[NSOutputStream alloc]initToFileAtPath:self.filePath append:YES];
[self.output open];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
self.currentSize += data.length;
CGFloat progress = (CGFloat)self.currentSize / self.fileSize;
NSLog(@"%f", progress);
dispatch_async(dispatch_get_main_queue(), ^{
self.progressBtn.progress = progress;
});
[self.output write:data.bytes maxLength:data.length];
NSLog(@"%@",[NSThread currentThread]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"下载完成了");
[self.output close];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"出错了");
}
@end
#import <UIKit/UIKit.h>
@interface ProgressButton : UIButton
@property (nonatomic, assign) CGFloat progress;
@property (nonatomic, assign) CGFloat lineWidth;
@end
#import "ProgressButton.h"
@implementation ProgressButton
- (void)setProgress:(CGFloat)progress {
_progress = progress;
self.lineWidth = 20;
[self setNeedsDisplay];
[self setTitle:[NSString stringWithFormat:@"%0.2f%%",_progress * 100] forState:UIControlStateNormal];
}
- (void)drawRect:(CGRect)rect {
CGFloat width = rect.size.width;
CGFloat height = rect.size.height;
CGPoint center = CGPointMake(width * 0.5, height * 0.5);
CGFloat radius = (MIN(width, height) - self.lineWidth) * 0.5;
CGFloat startAngle = - M_PI_2;
CGFloat endAngle = M_PI * 2 * self.progress + startAngle;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
[path setLineWidth:self.lineWidth];
[[UIColor redColor]set];
[path stroke];
}
@end