简体   繁体   中英

How to receiving data when the iOS app is in background mode

I have iOS app developers in Xamarin.Forms. I want to receive the request (from server) and want to execute it in iOS app, when it is in minimized / background mode. I have already played with Background Modes option available in Info.plist like "Audio, Airplay and Picture in Picture", "Voice over IP", "Background fetch", "Background Processing" but it didn't work for me.

If anyone have ideas above for this, then Kindly help me to resolve.

Thanks in advance,

Vivek

Having a look at Performing Tasks During DidEnterBackground .

In addition to making a long-running task background-safe, registration can be used to kick off tasks as an application is being put in the background. iOS provides an event method in the AppDelegate class called DidEnterBackground that can be used to save application state, save user data, and encrypt sensitive content before an application enters the background. An application has approximately five seconds to return from this method or it will get terminated. Therefore, cleanup tasks that might take more than five seconds to complete can be called from inside the DidEnterBackground method. These tasks must be invoked on a separate thread.

The process is nearly identical to that of registering a long-running task. The following code snippet illustrates this in action:

public override void DidEnterBackground (UIApplication application) {
  nint taskID = UIApplication.SharedApplication.BeginBackgroundTask( () => {});
  new Task ( () => {
    DoWork();
    UIApplication.SharedApplication.EndBackgroundTask(taskID);
  }).Start();
}

You can do something in DoWork method . By the way , I think apple not recommanding to work as a service to receive data . Generally , this background task is designed to deal with not finished transfering data , such as download or upload data .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM