//
// ViewController.m
// DownLoadBig
//
// Created by apple on 15/5/5.
// Copyright (c) 2015年 apple. All rights reserved.
//
#import "ViewController.h"
#import "CircularProgressView/CircleProgressView.h"
#import "CircularProgressView/CircleShapeLayer.h"
@interface ViewController () <NSURLConnectionDataDelegate,UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UIProgressView *proView;
@property (nonatomic,strong) NSMutableData *fileData;
@property (nonatomic,assign) long long total;
@property (nonatomic,strong) UIAlertView *alert;
@property (nonatomic,strong) NSFileHandle *wHandle;
@property (nonatomic,assign) long long currentLength;
@property (nonatomic,strong) NSString *urlstr;
@property (nonatomic,strong) CircleProgressView *cproView;
@property (weak, nonatomic) IBOutlet UIButton *downLoad;
@property (nonatomic,strong) NSURLConnection *conn;
@end
@implementation ViewController
- (IBAction)download:(id)sender {
_downLoad.selected = !_downLoad.isSelected;
if (_downLoad.selected){
NSURL *url = [NSURL URLWithString:self.urlstr];
// NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *range = [NSString stringWithFormat:@"bytes=%lld-",self.currentLength];
// [req setValue:range forKey:@"Range"];
[req setValue:range forHTTPHeaderField:@"Range"];
self.conn = [NSURLConnection connectionWithRequest:req delegate:self];
}
else
{
[self.conn cancel];
self.conn = nil;
}
}
- (NSString *)urlstr
{
if (_urlstr == nil){
_urlstr = @"http://125.39.68.200/files/2011000000F9723B/xiazai.888rj.com/Soft/T/Thunder_7.9.26.4824_XiaZaiBa.exe";
}
return _urlstr;
}
-(NSMutableData *)fileData
{
if (_fileData == nil){
_fileData = [NSMutableData data];
}
return _fileData;
}
- (void)viewDidLoad {
[super viewDidLoad];
// self.proView.progress = 0.0;
self.proView.progress = 0;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
if (self.currentLength) return;
NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
// NSLog(@"jieshou");
// NSLog(@"%@%",res.allHeaderFields[@"Content-Length"]);
// self.total= res.allHeaderFields[@"Content-Length"];
self.total = res.expectedContentLength;
NSString *AppName = [[self.urlstr componentsSeparatedByString:@"/"] lastObject];
NSString *cachestr = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [cachestr stringByAppendingPathComponent:AppName];
NSFileManager *mgr = [NSFileManager defaultManager];
[mgr createFileAtPath:filePath contents:nil attributes:nil];
self.wHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// [self.fileData appendData:data];
[self.wHandle seekToEndOfFile];// 寻找文件的末尾偏移量
[self.wHandle writeData:data];
self.currentLength += data.length;
self.proView.progress = (double)self.currentLength / self.total;
NSLog(@"%lld",self.currentLength);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// [self.fileData writeToFile:filePath atomically:YES];
// /Users/apple/Library/Developer/CoreSimulator/Devices/612DC518-63EA-4614-AA42-30CAECF2B57B/data/Containers/Data/Application/77095FDC-6D9B-4BB6-AC5F-460EB80DBD22/Library/Caches
long long totalMB = (self.total /1024/1024);
NSString *totalstr = [ NSString stringWithFormat:@"软件总大小:%lldMB",totalMB];
self.alert = [[UIAlertView alloc] initWithTitle:@"下载完成" message:totalstr delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil ];
[self.alert show];
}
@end