Xamarin- Nsurlsession UploadTask not working for iOS in background

241 views Asked by At

I am having a xamarin forms application in which I need to upload image files to the server and upload should continue even when the application goes to background. I have implemented nsurlsession CreateUploadTask with multipart-form data and I am calling this upload method in shared project using dependency service. But when I start to upload in foreground and tap on the home button after some time If I again tap on app icon then the app relaunches instead of resuming. My requirement is when upload completed in background trigger a notification "upload completed" and when user taps on app icon resume it to the previous screen. (Note some time for the first upload it works in the background for next request it relaunches the app)

Below is my code for session configuration-

public NSUrlSession InitBackgroundSession()
        {
            string identifier = "Myidentifier";
            using (var config = NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration(identifier))
            {
              
                config.HttpMaximumConnectionsPerHost = 4; //iOS Default is 4
                config.TimeoutIntervalForRequest = 3600.0; //30min allowance; iOS default is 60 seconds.
                config.TimeoutIntervalForResource = 3600.0; //2min; iOS Default is 7 days
                return NSUrlSession.FromConfiguration(config, new UploadDelegate() as INSUrlSessionDelegate, new NSOperationQueue());
            }
        }

Also if I add config.SessionSendsLaunchEvents = false; then the app does not get relaunch and if I tap on the app icon it resumes me to the previous screen and then calls DidReceiveData where I have written code to return response and send a notification. But I need to show notification in the background. None of the completion handler methods gets called when I set SessionSendsLaunchEvents=false.

Any help would be appreciated. Thanks!

0

There are 0 answers