Here is the code snippet when you need to run a task in background on an iOS device even when user locks his/her iPhone or iPad or iOS device.
Here is the code is written in AppDelegate.cs for background task.
- #region background Task
- public override void WillEnterForeground(UIApplication application) {
- Console.WriteLine("App will enter foreground");
- }
- // Runs when the activation transitions from running in the background to
- // being the foreground application.
- public override void OnActivated(UIApplication application) {
- // Console.WriteLine("App is becoming active");
- }
- public override void OnResignActivation(UIApplication application) {
- //Console.WriteLine("App moving to inactive state.");
- }
- public override void DidEnterBackground(UIApplication application) {
- //Console.WriteLine("App entering background state.");
- nint taskID = 0;
- // if you're creating a VOIP application, this is how you set the keep alive
- //UIApplication.SharedApplication.SetKeepAliveTimout(600, () => { /* keep alive handler code*/ });
- // register a long running task, and then start it on a new thread so that this method can return
- taskID = UIApplication.SharedApplication.BeginBackgroundTask(() => {
- Console.WriteLine("Running out of time to complete you background task!");
- UIApplication.SharedApplication.EndBackgroundTask(taskID);
- });
- Task.Factory.StartNew(() => FinishLongRunningTask(taskID));
- }
- void FinishLongRunningTask(nint taskID) {
- Console.WriteLine("Starting task {0}", taskID);
- Console.WriteLine("Background time remaining: {0}", UIApplication.SharedApplication.BackgroundTimeRemaining);
- // sleep for 15 seconds to simulate a long running task
- Thread.Sleep(15000);
- Console.WriteLine("Task {0} finished", taskID);
- Console.WriteLine("Background time remaining: {0}", UIApplication.SharedApplication.BackgroundTimeRemaining);
- // call our end task
- UIApplication.SharedApplication.EndBackgroundTask(taskID);
- }
- // [not guaranteed that this will run]
- public override void WillTerminate(UIApplication application) {
- Console.WriteLine("App is terminating.");
- }#endregion

tama ksaPosted Apr 12, 2021, 1:15 PM
Hi , how using Background get Location