.NET MAUI on iOS: Background NSUrlSession fails on Simulator

145 Views Asked by At

When initiating a download task from a background NSUrlSession I correctly get the data on actual iOS devices, but in the Simulator I always get an "unknown error" instead - in the debug console:

Task <1FCAE53C-7756-4408-8809-3AC3E7D45587>.<1> finished with error [-1] Error Domain=NSURLErrorDomain Code=-1 "unknown error" UserInfo={NSErrorFailingURLStringKey=https://fosdem.org/2024/schedule/, NSErrorFailingURLKey=https://fosdem.org/2024/schedule/, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "BackgroundDownloadTask <1FCAE53C-7756-4408-8809-3AC3E7D45587>.<1>"
), _NSURLErrorFailingURLSessionTaskErrorKey=BackgroundDownloadTask <1FCAE53C-7756-4408-8809-3AC3E7D45587>.<1>, NSLocalizedDescription=unknown error}

You can find the invocation here: https://github.com/sebug/MauiBackgroundURLSession/blob/main/MainPage.xaml.cs and here is the handler inheriting NSSessionDownloadDelegate https://github.com/sebug/MauiBackgroundURLSession/blob/main/Platforms/iOS/DownloadHandler.cs

I expected the iOS Simulator to also process the download request or at least give me an error that I can use to get to the bottom of the problem.

I tried invoking the call during different parts of the app lifecycle (directly on finished loading of the App Delegate, after clicking on an UI element, as a result of a WeakReferenceMessenger.Default.Send, etc.). It doesn't seem to make a difference.

I added the fetch string in the UIBackgroundModes on the Info.plist as well, which makes a warning at startup disappear but doesn't help. My hypothesis is that somehow the simulator does not know about this entitlement after all, and straight up refuses to perform the BackgroundDownloadTask.

0

There are 0 best solutions below