如何在 react-native iOS 的后台继续下载多个文件(AWS 服务器映像)



我创建了一个应用程序,用户可以在其中使用react-native-fs一键下载多个图像,这在Android中运行良好。但是在iOS中,当应用程序处于非活动状态时,下载停止,用户必须再次开始下载。

async.eachSeries(DownloadData, async function (tourData, finish) {
    console.log("# resumable : 655612", tourData);
    var fileExtension = '';
    var fileURL = tourData.path;
var fileExtension = "/" + tourData.name + "Image" + p + ".png
     p = p + 1;
    const downloadDest = RNFS.DocumentDirectoryPath + fileExtension;
    let dirs = RNFetchBlob.fs.dirs;
    var v = dirs.DocumentDir;
    var jobId = -1;
    const ret = RNFS.downloadFile({
        fromUrl: encodeURI(fileURL),
        toFile: downloadDest,
        connectionTimeout: 1000 * 10,
        readTimeout: 1000 * 10,
        background: true,
        discretionary: true,
        progressDivider: 1,
        resumable: (res) => {
            console.log("# resumable", res);
        },
        begin: (res) => {
         console.log(res)
        },
        progress: (data) => {
           console.log(data)
        },
    });
    jobId = ret.jobId;
    RNFS.isResumable(jobId).then(true);
    if (await RNFS.isResumable(jobId)) {
        console.log("# resumable : # resumable : # resumable :",jobId);
        RNFS.resumeDownload(jobId)
    }
    ret.promise.then((res) => {
       finish();
   }).catch(err => {
       finish();
   })

},function (err) { if (!err) { 回调(真) } else { 回调(假) }}));

在 IOS 的后台运行下载需要一些额外的设置,请查看此部分 https://github.com/itinance/react-native-fs#background-downloads-tutorial-ios他们还提到,IOS会在handleEventsForBackgroundURLsession后给你30秒

的时间。

BE AWARE! iOS will give about 30 sec. to run your code after handleEventsForBackgroundURLSession is called and until completionHandler is triggered so don't do anything that might take a long time (like unzipping), you will be able to do it after the user re-launces the app, otherwide iOS will terminate your app.

我希望这有帮助

相关内容

  • 没有找到相关文章

最新更新