Home/Project tweaks, Uncategorized/Providing background services support to your iOS apps with Delphi

Providing background services support to your iOS apps with Delphi

Views:
72

If your iOS app needs to perform some work while it is running in the background on iOS, you’ll need to do some special “tweaking”, since the functionality is not provided for in the IDE.

When Delphi compiles your iOS app for the selected release type (i.e. either Debug or Release), it creates a file called <appname>.info.plist, where <appname> is the name of your application. This file is deployed to iOS as Info.plist and is the file that iOS uses to determine whether your app uses any background services. The types of background services supported on iOS is described in the Apple iOS documentation here, however to summarize, they are:

Mode
UIBackgroundModes value
Description
Audio audio The app plays audible content to the user or records audio while in the background. (This content includes streaming audio or video content using AirPlay.)
Location updates location The app keeps users informed of their location, even while it is running in the background.
Voice over IP voip The app provides the ability for the user to make phone calls using an Internet connection.
Newsstand downloads newsstand-content The app is a Newsstand app that downloads and processes magazine or newspaper content in the background.
External accessory communication external-accessory The app works with a hardware accessory that needs to deliver updates on a regular schedule through the External Accessory framework.
Bluetooth networking bluetooth-central The app works with a Bluetooth accessory that needs to deliver updates on a regular schedule through the Core Bluetooth framework.
Bluetooth data sharing bluetooth-peripheral The app supports Bluetooth communication in peripheral mode through the Core Bluetooth framework.Using this mode requires user authorization; for more information, see “Best Practices for Maintaining User Privacy.”
Background fetch fetch The app regularly downloads and processes small amounts of content from the network.
Remote notifications remote-notification The app’s response to a push notification includes optionally downloading content related to the notification. The purpose of downloading the content in the background is to incorporate it and be able to present it immediately when the user responds to the push notification.

In order for your app to support background processing for any or all of these services, you’ll need to create a copy of the <appname>.info.plist file (note: you’ll need to compile at least once in order for the IDE to create the file) and manually edit it, because Delphi overwrites the original file every time you compile your app. The file also contains other information that will change if you change certain project options, so you’ll need to keep that in mind so that you can re-insert the background services information.

Once you’ve copied the <appname>.info.plist file (it makes sense to have it in the same directory, or even in the project root directory), you can edit the file with a “unix-eol-friendly” editor such as Notepad++, and anywhere inside the <dict> tag, add the following (I usually place it just before the end </dict> tag), for example:

<key>UIBackgroundModes</key>
 <array>
   <string>location</string>
 </array>

You’ll need to add a separate <string> key for each service your app will support. In the Delphi deployment manager (click Project|Deployment in the main menu), uncheck the original <appname>.info.plist:

then click the little yellow “Add Files” button at the top left of the Deployment window, select the file you copied and edited earlier and click Open. For the file you just added, click the name of the file in the Remote Name column, click it again to put it into edit mode:

and rename it to Info.plist. (make sure it has a capital “I” at the beginning). Make sure you save these changes.

Now when Delphi deploys your app, the new Info.plist file will be deployed along with it. Once again, remember that if you make changes to some project options, such as the icons, or the version info, you may need to go through this process again, since those changes will be in the original <appname>.info.plist file. Perhaps in the near future someone might come up with an expert to manage this automatically.

By | 2017-02-16T18:02:36+00:00 October 14, 2013 10:36 am|Project tweaks, Uncategorized|8 Comments

About the Author:

8 Comments

  1. Rene October 23, 2013 at 1:11 am - Reply

    hello, I tried to do as you said, but my app does not run in the background. Will need to change any other attribute of the Info.plist?

    Thank you,
    Rene

    • admin October 23, 2013 at 5:54 am - Reply

      What background mode are you using? What code do you have that is meant to run in the background?

  2. Magnus October 25, 2013 at 10:45 pm - Reply

    Hi, I tried with this as well with “fetch”, but it does not seem to help. If I have a TTimer, it stops getting OnTimer events when the app goes into the background, and/or a TThread stops executing. I tried disabling and then re-enabling the TTimer in the aeEnteredBackground app event but that did not help. Is there something additional needed?

    Thanks,
    Magnus

    • admin October 28, 2013 at 10:09 am - Reply

      As you’ve discovered, using TTimer and TThread will not work. The following links describe how to implement fetch backgorund mode in Objective C:

      http://www.doubleencore.com/2013/09/ios-7-background-fetch/
      http://hayageek.com/ios-background-fetch/

      However doing this in Delphi might be not quite straightforward, as some declarations are missing. I’ll revisit the topic once I’ve examined how this might be done.

      • Magnus October 28, 2013 at 5:21 pm - Reply

        Hi and thanks for your answer! For now I could settle with any workaround that allows background processing (and preferably also longer than 10 minutes which I saw when I tried with a different tool), the app is for demo only at this point. E.g. would it be easier if I used location tracking even if I don’t really need it? All I need is some way where I can hook in some code performing a http request at regular intervals and possibly scheduling a local notification.

        • admin October 28, 2013 at 5:27 pm - Reply

          I believe it would only be possible to use location background mode if the user actually moves far enough to trigger a location change. I have heard that others have used audio background mode and play a sound file that contains silence in order to keep the app running in the background, but these apps might not be accepted on the app store. I’ll look further into it.

  3. Pengzhi March 14, 2014 at 11:24 am - Reply

    Hey Dave ! Here is an easier way to do that :

    Project Options
    Versin Info
    ListView of Key & Value
    Mouse right click
    Add Key !!! Add then UIBackgroundModes KEY !!!!

  4. Pengzhi March 14, 2014 at 12:52 pm - Reply

    Create a key named ‘Dummy001’, copy follows to key value:

    Dummy001

    <—–begin copy at this line
    UIBackgroundModes

    audio
    voip
    fetch
    remote-notification

    Dummy002
    <—–end copy at this line

Leave a Reply

Show Buttons
Hide Buttons