如何在本机iOS中继续在后台下载多个文件(AWS服务器映像)

时间:2019-01-24 04:56:51

标签: react-native react-native-android react-native-ios react-native-fs ios-background-mode

我创建了一个应用程序,用户可以使用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();
   })

},函数(错误){     如果(!err){         回调(true)     }其他{      回调(false)     } }));

1 个答案:

答案 0 :(得分:0)

在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.

我希望这对您有帮助