gpt4 book ai didi

ios - 即使在终止并重新启动应用程序后,如何在 native iOS 中下载音频文件?

转载 作者:行者123 更新时间:2023-12-04 04:05:21 24 4
gpt4 key购买 nike

我的应用程序中有一个场景是使用 React Native (0.59) 开发的,我正在尝试下载音频文件。即使用户杀死并重新启动它,我也想继续下载。我正在尝试'react-native-background-downloader' 包。即使在终止并重新启动应用程序后,我也会在模拟器中下载和播放。但不是在实际设备中。任何建议将不胜感激。

这是我正在使用的代码块

let task = RNBackgroundDownloader.download({
id: fileName,
url: fileUrl,
destination: `${RNBackgroundDownloader.directories.documents}/${fileName}`,

}).begin((expectedBytes) => {
console.log(`Going to download ${expectedBytes} bytes!`);
}).progress((percent) => {
console.log(`Downloaded: ${percent * 100}%`);
}).done(() => {
console.log('Download is done!');
}).error((error) => {
console.log('Download canceled due to error: ', error);
});

这是我的简历任务

 let lostTasks = await RNBackgroundDownloader.checkForExistingDownloads();
for (let task of lostTasks) {
console.log(`Task ${task.id} was found!`);
task.progress((percent) => {
console.log(`Re-Downloaded: ${percent * 100}%`);
}).done(() => {
console.log('Re-Downlaod is done!');
}).error((error) => {
console.log('Re-Download canceled due to error: ', error);
});
}

我能够看到重新下载:99%,然后我收到类似这样的错误'重新下载因错误而取消:','“CFNetworkDownload_jtLLac.tmp”无法移动到“文档”,因为前者不存在,或者包含后者的文件夹不存在。

最佳答案

iOS - 额外的强制步骤

在您的 AppDelegate.m 中添加以下代码:

..
#import <RNBackgroundDownloader.h>

...

- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler
{
[RNBackgroundDownloader setCompletionHandlerWithIdentifier:identifier completionHandler:completionHandler];
}

...

未能添加此代码将导致后台下载被取消。

并且仅使用 RNBackgroundDownloader.directories.documents 作为目标。

关于ios - 即使在终止并重新启动应用程序后,如何在 native iOS 中下载音频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62593705/

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