2012-08-29 35 views
6

Tôi cần tải xuống các tệp .zip lớn (tối đa 800 MB) bằng ứng dụng iPad của mình. Tôi muốn tiếp tục tải xuống lại nếu nó bị hủy hoặc nếu ứng dụng ở chế độ nền.AFNetworking + Pause/Tiếp tục tải xuống các tệp lớn

operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:YES]; 

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){ 

// unzip the .zip 


}failure:^(AFHTTPRequestOperation *operation, NSError *error){ 

//Handle the error 

}]; 


[operation setDownloadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten,long long totalBytesExpectedToWrite) { 

//update the progressView 

}]; 

[operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{ 

// What I have to do here? 

}]; 

[operation start]; 
[operation waitUntilFinished]; 


-(void)applicationWillResignActive:(UIApplication *)application{ 

// What I have to do here? 

} 

Cảm ơn sự giúp đỡ của bạn.

Trả lời

23

Bạn có thể sử dụng AFDownloadRequestOperation để thực hiện việc này.

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"....zip"]]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"....zip"]; 
    AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:path shouldResume:YES]; 
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"Successfully downloaded file to %@", path); 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Error: %@", error); 
    }]; 
    [operation setProgressiveDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) { 
     NSLog(@"Operation%i: bytesRead: %d", 1, bytesRead); 
     NSLog(@"Operation%i: totalBytesRead: %lld", 1, totalBytesRead); 
     NSLog(@"Operation%i: totalBytesExpected: %lld", 1, totalBytesExpected); 
     NSLog(@"Operation%i: totalBytesReadForFile: %lld", 1, totalBytesReadForFile); 
     NSLog(@"Operation%i: totalBytesExpectedToReadForFile: %lld", 1, totalBytesExpectedToReadForFile); 
    }]; 
    [operations addObject:operation]; 

Sau khi bạn khởi động lại ứng dụng và tạo yêu cầu có cùng url, nó sẽ tiếp tục tải xuống. "shouldResume: YES" hoạt động

+1

Có phương án không ARC nào khác không? – Tudorizer

+0

Bạn có biết điều này có hoạt động với chuyển hướng URL không? Tôi đã đặt url rồi chuyển hướng đến tệp thực để tải xuống. Tải xuống dường như không tiếp tục sử dụng các URL này. –

+0

Chỉ cần đảm bảo rằng bạn chỉ định cùng một targetPath mỗi lần. Ngoài ra nó không thể là thư mục; nếu bạn chỉ định một thư mục, nó sẽ sử dụng URL để đặt tên cho tệp. – mrgrieves

Các vấn đề liên quan