Breakpoint download, android breakpoint download
1 @ interface ViewController () <NSURLConnectionDataDelegate> 2 {3 // total file size 4 long _ total; 5 // The current download 6 long _ current; 7} 8 9 // file handle 10 @ property (nonatomic, strong) NSFileHandle * fileHandle; 11 // connection object 12 @ property (nonatomic, strong) NSURLConnection * conn; 13 @ property (weak, nonatomic) IBOutlet UIProgressView * progress; 14 @ property (weak, nonatomic) IBOutlet UIButton * button; 15 @ end16 17 @ implementation ViewController18-(void) viewDidLoad {19 [super viewDidLoad]; 20 self. progress. progress = 0; 21 // Do any additional setup after loading the view, typically from a nib.22} 23 24-(IBAction) buttonAction :( UIButton *) sender {25 if ([[sender titleForState: UIControlStateNormal] isecontostring: @ ""]) {26 [sender setTitle: @ "Suspend" forState :( UIControlStateNormal)]; 27 // request 28 NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: kUrl]; 29 // Set request Parameter 30 NSString * rang = [NSString stringWithFormat: @ "byte: % lld ", _ current]; 31 NSLog (@" request: % @ end ", request); 32 [request setValue: rang forHTTPHeaderField: @" Range "]; 33 NSLog (@ "request: % @ end", request); 34 // establish a connection 35 self. conn = [NSURLConnection connectionWithRequest: request delegate: self]; 36 // start request 37 [_ conn start]; 38} else {39 [sender setTitle: @ "start" forState :( UIControlStateNormal)]; 40 // cancel 41 [_ conn cancel]; 42} 43} 44 45-(void) didReceiveMemoryWarning {46 [super didreceivemorywarning]; 47 // Dispose of any resources that can be recreated.48} 49 50 # pragma mark --- delegate51 // receives the response 52-(void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response53 {54 // get the total file size (the total file size can be obtained in the response) 55 _ total = response. expectedContentLength; 56 // Create File 57 NSFileManager * manager = [NSFileManager defaultManager]; 58 NSString * cachesPath = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) [0]; 59 NSString * filePath = [cachesPath stringByAppendingPathComponent: @ "musicloud"]; 60 NSLog (@ "% @", cachesPath); 61 // Create File 62 [manager createFileAtPath: filePath contents: nil attributes: nil]; 63 // point the file handle to the file 64 self. fileHandle = [NSFileHandle fileHandleForWritingAtPath: filePath]; 65} 66 // receive data 67-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data68 {69 // calculate the total current size of 70 _ current + = data. length; 71 // Update (UI cannot be updated in the Child thread) 72 [self generated mselecw.mainthread: @ selector (updateProgress) withObject: self waitUntilDone: YES]; 73 // point to 74 [self. fileHandle seekToEndOfFile]; 75 // write data 76 [self. fileHandle writeData: data]; 77} 78 // end 79-(void) connectionDidFinishLoading :( NSURLConnection *) connection80 {81 NSLog (@ "downloaded "); 82} 83 // update progress84-(void) updateProgress85 {86 self. progress. progress = (long double) _ current/(long double) _ total; 87} 88 89 @ end