On iOS, UnityWebRequest download stops when app goes to background

I use UnityWebRequestAssetBundle.GetAssetBundle to download asset bundles located on a remote server. While everything works as expected on Android, the download stops on iOS when minimizing the app (or when the device goes to sleep).

When I foreground the app again, XCode shows the following logs:

-> applicationWillResignActive()
-> applicationDidEnterBackground()
2020-03-08 08:37:51.571235+0100 app[3852:2943256] Can't end BackgroundTask: no background task exists with identifier 1 (0x1), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
-> applicationWillEnterForeground()
2020-03-08 08:37:55.169337+0100 app[3852:2943564] [] nw_read_request_report [C4] Receive failed with error "Software caused connection abort"
2020-03-08 08:37:55.194339+0100 app[3852:2943564] Task <015CA857-5213-4905-8F1B-4A0E3DA330DE>.<5> HTTP load failed, 350/5095859 bytes (error code: -1005 [1:53])
2020-03-08 08:37:55.195303+0100 app[3852:2943564] Task <446A65DA-5ACF-493C-AE8F-B746364F4A9E>.<6> HTTP load failed, 693/0 bytes (error code: -1005 [1:53])
2020-03-08 08:37:55.197007+0100 app[3852:2943431] Task <015CA857-5213-4905-8F1B-4A0E3DA330DE>.<5> finished with error [-1005] Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={_kCFStreamErrorCodeKey=53, NSUnderlyingError=0x2800ced00 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={NSErrorPeerAddressKey=<CFData 0x282ca0be0 [0x1e218bcf0]>{length = 16, capacity = 16, bytes = 0x10021f40c0a801300000000000000000}, _kCFStreamErrorCodeKey=53, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <015CA857-5213-4905-8F1B-4A0E3DA330DE>.<5>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <015CA857-5213-4905-8F1B-4A0E3DA330DE>.<5>"
), NSLocalizedDescription=The network connection was lost., NSErrorFailingURLStringKey=http://192.168.1.48:8000/Mobile/dlc1, NSErrorFailingURLKey=http://192.168.1.48:8000/Mobile/dlc1, _kCFStreamErrorDomainKey=1}

C# code:

public static AssetBundleDownloadProgress DownloadBundle (string bundleName, bool forImmediateLoad, DownloadBundleFinishedDelegate finished, DownloadBundleErrorDelegate error)
{
    UnityWebRequest wr;
    
    if (HashDictionaryRemote.ContainsKey(bundleName))
    {
        uint crc = CRCDictionaryRemote[bundleName];
        Hash128 hash = HashDictionaryRemote[bundleName];
        
        wr = UnityWebRequestAssetBundle.GetAssetBundle (SERVER_URL + bundleName, hash, crc);
    }
    else
    {
        wr = UnityWebRequestAssetBundle.GetAssetBundle (SERVER_URL + bundleName);
    }

    wr.disposeDownloadHandlerOnDispose = true;
    wr.SetRequestHeader ("Cache-Control", "no-cache, no-store, must-revalidate");
    wr.SetRequestHeader ("Pragma", "no-cache");
    wr.SetRequestHeader ("Expires", "0");

    AssetBundleDownloadProgress progress = new AssetBundleDownloadProgress(wr);
    
    Instance.StartCoroutine(DownloadBundleCoroutine (wr, forImmediateLoad, progress, bundleName, finished, error));
    
    return progress;
}

private static IEnumerator DownloadBundleCoroutine (UnityWebRequest wr, bool forImmediateLoad, AssetBundleDownloadProgress progress, string bundleName, DownloadBundleFinishedDelegate finished, DownloadBundleErrorDelegate error)
{
    yield return wr.SendWebRequest();

    if (wr.isNetworkError) 
    {
        error (wr.error);
    } 
    else
    {
        AssetBundle bundle = ((DownloadHandlerAssetBundle)wr.downloadHandler).assetBundle;

        if (bundle == null) 
        {
            error.Invoke($"Error loading bundle {bundleName}, probably another bundle with same files is already loaded.");
        } 
        else
        {
            finished.Invoke(bundle, progress);
            
            if(!forImmediateLoad)
                bundle.Unload(true);
        }
    }
}

I use Unity 2019.2

Any help would be much appreciated! :slight_smile:

I have the same problem. Did you manage to solve it?