gpt4 book ai didi

ios - 如何在 AFNetworking 3 中使用流媒体下载大文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:23:50 28 4
gpt4 key购买 nike

我想使用 AFNetworking 3 下载大文件。但我需要在出现任何中断(如网络丢失或任何其他情况)时继续下载。如果下载时有任何中断,我想从它提前停止的地方开始下载。是否可以使用 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];

最佳答案

在这里阅读更多相关信息: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];


}

关于ios - 如何在 AFNetworking 3 中使用流媒体下载大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37698417/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com