Скачать несколько аудио файлов
Я пытаюсь загрузить несколько файлов.mp3 с сервера одновременно. Один полный аудиофайл разделен на 286 частей. Я извлекаю все URL файла и теперь хочу скачать 286 файлов. Я ищу много, но многие библиотеки перестают загружаться, когда я возвращаюсь к предыдущему контроллеру, и если пользователь свернул приложение, загруженная остановка. Существует ли какая-либо библиотека, которая может управлять несколькими загрузками, и загрузка не прекращалась, когда пользователь возвращался к предыдущему контроллеру для минимизации приложения. Я использую библиотеку Download Manager, но не могу получить желаемое. Пожалуйста, дайте мне решение. Я застрял с этим от 3 дней. Пожалуйста, скажите мне решение. Спасибо
1 ответ
В моем проекте я использую AFNetwirking. Вы можете создать одноэлементный объект, и вот мой метод (например) для загрузки файлов:
- (AFHTTPRequestOperation *)performDownloadWithURLString:(nonnull NSString *)urlString
destinationPath:(NSString *)destinationPath
progress:(void (^)(long long bytesRead, long long totalBytesToRead))progress
apiCallback:(void(^)(BOOL isSuccessful, id object))apiCallback
{
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSString *fullPath = [[FCFileManager pathForTemporaryDirectory] stringByAppendingPathComponent:[url lastPathComponent]];
[operation setOutputStream:[NSOutputStream outputStreamToFileAtPath:fullPath append:NO]];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead)
{
if(progress) {
progress(totalBytesRead, totalBytesExpectedToRead);
}
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if(destinationPath && [FCFileManager isFileItemAtPath:destinationPath]) {
NSError *removeError;
[FCFileManager removeItemAtPath:destinationPath error:&removeError];
}
if(destinationPath) {
[FCFileManager moveItemAtPath:fullPath toPath:destinationPath];
}
dispatch_async(dispatch_get_main_queue(), ^{
if(apiCallback) {
apiCallback(YES, destinationPath ?: fullPath);
}
});
});
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSError *removeError;
[FCFileManager removeItemAtPath:fullPath error:&removeError];
if(apiCallback) {
apiCallback(NO, [AZError errorWithNSError:error]);
}
}];
[operation start];
}
Надеюсь, это поможет вам.