Search code examples
iosiphonebackgroundxamarinbackground-task

Xamarin background task when connectivity changes


Im working on a project which requires me to do some uploading when the connectivity changes. Im a writing an android and iOS app using Xamarin. I know in iOS there are huge restrictions using background tasks. Also in iOS there is no way, unlike android, to subscribe to a receiver such as connectivitychanged.

So im looking for a work around without abusing different API's. I don't mind a scheduled task every n minutes, but im not sure if this is possible.

What i've done so far:

  • Tried using background fetch, seems to be very irregular and unable to predict when the task will be fired.
  • Background transfer task, but can't find a way to run this in the background periodically.
  • using the background location to check when the location changes and then check for connectivity and if so do some uploading. However i think this is abusing the API, and would not get past the app store.

Is there any work arounds that people have come across. I don't mind trying to convert iOS code into xamarin/c#.

Thanks.


Solution

  • You may have solved this already but here's what I did (Xamarin Forms)...

    I simply used: http://www.nuget.org/packages/Xam.Plugin.Connectivity Installed it in my Common and Platform specific projects

    and in my Application.cs, right after I set the MainPage, I used

    Connectivity.Plugin.CrossConnectivity.Current.ConnectivityChanged += async (sender, args) => 
    {
    if (args.IsConnected)
                {
                   ...
                   ...
                }
    ... 
    }