2

我想使用 AFNetworking 3 下载大文件。但是我需要在出现任何中断(如 Internet 丢失或任何其他情况)时恢复下载。如果下载时出现任何中断,我想从它提前停止的地方开始下载。是否可以使用 AFNetworking 或我是否使用任何其他库?请任何人帮助我。这是我的代码。

NSURLRequest *request = [NSURLRequest requestWithURL:formattedURL];

//Watch the manager to see how much of the file it's downloaded
[manager setDownloadTaskDidWriteDataBlock:^(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
    //Convert totalBytesWritten and totalBytesExpectedToWrite into floats so that percentageCompleted doesn't get rounded to the nearest integer
    CGFloat written = totalBytesWritten;
    CGFloat total = totalBytesExpectedToWrite;
    CGFloat percentageCompleted = written/total;

    //Return the completed progress so we can display it somewhere else in app
    //progressBlock(percentageCompleted);
    NSLog(@"Percentage Completed : %f",percentageCompleted);
    [self updateProgressBar:percentageCompleted];
}];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
    //Getting the path of the document directory
    NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
    NSURL *fullURL = [documentsDirectoryURL URLByAppendingPathComponent:@"3511_1464694276.zip"];

    //If we already have a video file saved, remove it from the phone
    return fullURL;
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    if (!error) {
        //If there's no error, return the completion block
        //completionBlock(filePath);
    } else {
        //Otherwise return the error block
        //errorBlock(error);
    }

}];

[downloadTask resume];
4

1 回答 1

5

在此处阅读更多信息:https ://github.com/AFNetworking/AFNetworking

当你编写了一些代码或自己尝试了一些东西时,试着提出问题。

下载进度文件:

- (IBAction)downloadAudio:(id)sender {



    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    NSURL *URL = [NSURL URLWithString:@"http://101songs.com/fileDownload/Songs/0/26958.mp3"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {

        dispatch_async(dispatch_get_main_queue(), ^{
            //Update the progress view
            [_myProgressView setProgress:downloadProgress.fractionCompleted];

        });



    } destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
        return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        // Do operation after download is complete

    }];
    [downloadTask resume];


}
于 2016-06-08T09:34:43.303 回答