3

In our app users can track and submit journeys they have recorded. I need a simple way of creating a task in iOS. I have already created and tested this on Android. It works via:

  • The user selects the journeys they would like to submit.
  • Taps sync and a foreground service is created that syncs the journeys to our API.
  • This service will continue to sync journeys even if the app is put into the background or even closed.

So in short how can i achieve this on iOS 9-13?

I have already tried creating a background-safe tasks using:

nint taskID = UIApplication.SharedApplication.BeginBackgroundTask( () => {});

However, this only gives the task 2 mins to run which isn't enough. I have looked into the NSURlSessions but they require URls, whereas we are using our own API wrapper.

I would simply like a way of creating a task when the user taps 'sync' and this task also being capable to run in the background. I am not too bothered if the task is cancelled when the app is closed, although if possible would like the task to continue.

Thanks.

Jhardy
  • 89
  • 1
  • 10

3 Answers3

1

This service will continue to sync journeys even if the app is put into the background or even closed.

First, if your app is closed in iOS, I'm sure do can't run any service in background.

Then if your app is put into background, Apple has strict limit to allow apps running in the background. background-tasks has time limits, you can read the document about more information. There is a section about handling-background-task-time-limits which you can have a try.

Also, Apple allows some specific apps to run in background which have to perform tasks in the background. For example, app that needs to play music in background, update location in background and etc. You can see the Application Registration Categories here. If your app meets the requirement there, you can apply for a background running permission from system.

Refer: Backgrounding in Xamarin.iOS

Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57
nevermore
  • 15,432
  • 1
  • 12
  • 30
  • I have already read all of the Xamarin documentation and my request doesn't really fit into any of the categories. All i want is create a task that can run so the user doesn't have to wait for their journeys to sync.Surely this can be achieved somehow? I can't be the only one who's requirement match this criteria. – Jhardy Oct 11 '19 at 08:39
  • @Jhardy If the sync action takes very long time in background, Apple will not allow this as the time limit written in the document. – nevermore Oct 11 '19 at 08:46
  • Okay thank you, the task will take up to 5 minutes depending on the length of the journey. Seems like the only solution is too have the user sit and wait unfortunately. – Jhardy Oct 11 '19 at 09:50
  • Apple is always strict to background task in iOS. You can upvote/mark the answer if it is helpful to you. – nevermore Oct 11 '19 at 09:56
  • Yeah it seems a little over the top on Apples behalf, but oh well. Thanks. Upvoted. – Jhardy Oct 11 '19 at 10:00
0

I would advise you to leverage on Shiny to achieve it.

Elton Santana
  • 950
  • 3
  • 11
  • 22
0

PerformFetch is the closest thing to what you ask for, it will run in the background and update your app when iOS thinks it is needed (it predicts that according to the previous behavior the user will soon open your app and that the new content is available).

The only alternative is to send the push notification when you want the app to be updated.

That's about it, I understand your wish but it is just that - a wish and not something that can be real.

Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57
  • The app isn't being updated, i just simply want to run a task in the background so that the user doesn't have to sit and wait for their journeys to sync with the app open. Are Apple really that strict that this can't be achieved? – Jhardy Oct 11 '19 at 08:30
  • Did you read PerformFetch description? It literally says what you said above. Yes, that is the only way on iOS. – Ivan Ičin Oct 11 '19 at 08:56
  • Background Fetch - "Refresh an application from the background at system-determined intervals". I'm not refreshing the application at intervals, the user is kicking off the task. This could be everyday, every other day or once a week. Seems like the only option for iOS to have the user wait for the journeys to sync. – Jhardy Oct 11 '19 at 09:40