4 Replies
      Latest reply: Sep 2, 2016 12:05 PM by ukeyeee RSS
      ukeyeee Level 1 Level 1 (0 points)

        Hi guys, I got one issue about sending data with interval 1000 seconds to the server, while the app has been killed by user. I found the relevant document about app in background mode. https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html but I checked the document, it seems there is no way to use NSTimer inside the block of beginBackgroundTaskWithName... function. NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: #selector(AppDelegate.handleTimer), userInfo: nil, repeats: true) Question: How to consistently send data to server with interval time while app has been terminated? Thank you, e

        • Re: Sending data to server in background mode(killed app state)
          eskimo Apple Staff Apple Staff (7,005 points)

          How to consistently send data to server with interval time while app has been terminated?

          You don’t.  There are two parts to this:

          • If the user removes your app from the multitasking UI, the system interprets that as a strong signal that the user does not want your app running in the background [1].  That state persists until the user next runs your app manually.

          • Even if you ignore the above, iOS does not provide a general way for apps to run periodically in the background.  There are specific mechanism that do allow you to run in the background, but no general purpose way to get background execution time every N minutes.

          … sending data with interval 1000 seconds to the server …

          Just to be clear, you are not going to be able to accomplish this task.  You will have to rethink your approach.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

          [1] The system may actually run your app in the background in this state under specific circumstances, but this general rule applies.

            • Re: Sending data to server in background mode(killed app state)
              ukeyeee Level 1 Level 1 (0 points)

              Hi Eskimo

               

              Thank you for the quick explanation on my questions for sending generic data to the server in a consistent pace while the app has been terminated. I read through the document on background modes for apps for the link I attached above.

               

              From my understanding of your explanation, beginBackgroundTaskWithName , WithExpirationTime function won't work in actually background mode but instead in a short period of moment when the user killed the app. And it also confused me a little bit on the approach of dealling with general data, it documented as "Background fetch".

               

              The app regularly downloads and processes small amounts of content from the network.

              It seems the fetch method would get the signal from system and wait to be awaken while user next time runs the app. -> no way to execute the fetch on the background(app has been killed), let alone in a regular(periodly) time.

               

              Considering special cases such as bluetooth and corelocation data, bluetooth for example, Uses Bluetooth LE accessories bluetooth-central

              The app works with a Bluetooth accessory that needs to deliver updates on a regular schedule through the Core Bluetooth framework. (https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html)

               

              I believe it could be accomplished in corebluetooth framework to send the data in background mode on regular basis. If you have relevent latest sample demo code regarding my issue, would be appreciated if you could attach the link here.

               

              Warm regards,

              E

                • Re: Sending data to server in background mode(killed app state)
                  Beast Level 3 Level 3 (125 points)

                  Backgrounding and killing an app are two different things.  Backgrounding an app does not kill the app if the app is configured to run in the background.

                   

                  As to using BLE to wake the app, a connected BLE device would be needed to wake the app on a regular basis.

                   

                  For power saving reasons, iOS does not support polling like you want.  You need to rethink your architecture.  What are you really trying to do?

                    • Re: Sending data to server in background mode(killed app state)
                      ukeyeee Level 1 Level 1 (0 points)

                      Thank you for correcting my understanding about the background mode. My team lead mislead me on the understanding of the background mode. Originally I thought background mode is staging on user pressed the home buttom, and it would be miracle that developer could still operating the code in the dead ios app. But since the android developer implemented that mode and named it background mode. Therefore, my lead the background mode is the stage that app has been killed, and I need to sending data at interval time during that stage.

                       

                      Based on your statement, I think it makes much sense. Then I assume, all the apple documents that indicate the Background Mode is actually the stage that the user pressed the home buttom.

                       

                      Thanks again for your time.