I have an iOS app that automatically prints a receipt to a thermal receipt printer when an order comes in and works great. However, if the app is in the background, it does not print. Is it possible to allow it to print while running in the background?
2 Answers
There are only a few occasions you are allowed to run certain tasks in the background. See the Apple documentation: https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW20
For example: playing audio, fetching the user location, sending notification or certain data from network in the background is allowed. Running your own logic during a background service is, as far as I understood, not allowed (like running it one hour after your app is gone to sleep/background mode).
The only thing that is perhaps an option, is to utilize the time your app has to finish an task after the home button is pressed. There is a timelimit however of 10 or 15 minutes. I don't know if this is suitable for your situation? See this stackoverflow question: iOS application executing tasks in background And this particular piece of code to run the task in the background: https://stackoverflow.com/a/11809211/2740112

- 1
- 1

- 43
- 4
-
In iOS 7 the background task limit has been reduced to 3 minutes. To the OP, perhaps you could post a notification when an order is received, prompting the user to return to your app to print it. – Paulw11 Apr 10 '14 at 20:43
-
If it is indeed limited, I would go with the suggestion of Paulw11. Not sure if you could use push messages for incoming orders? – user2740112 Apr 11 '14 at 08:25
-
Depending on where your orders are coming from. You can use APNS push messaging or there is a background fetch mode available under iOS7 - your app wakes up periodically and could check a server for new orders - it can then post a local notification. This may be simpler than setting up the various certificates and registering devices, although a service like parse.com makes it easier – Paulw11 Apr 11 '14 at 08:43
Another option is to use Guided Access, which is introduced since iOS6. With it, you can prevent exiting the app. It's only usefull when using only the app on the device that needs to be used. Information below from http://www.assistiveware.com/support/faq/page/136:
To turn on Guided Access, do the following:
Go to the Settings app on your device's home screen.
Tap General.
Tap Accessibility.
Under the Learning section (scroll down if necessary), tap Guided Access.
Toggle Guided Access to ON.
Tap Set Passcode and enter a four digit passcode. You will be prompted to enter it again.
(Optional) Toggle Enable Screen Sleep to ON if you want to be able to put your device to sleep with the Power button, otherwise the Power button will be disabled.
To start Guided Access for an app and prevent it from being exited, do the following:
Open the app that you want to lock in.
Quickly press your device's Home button three times to bring up the Guided Access menu.
Tap the Start button in the top right corner of the screen to activate Guided Access. A message stating "Guided Access Started" will briefly appear.
To end Guided Access for an app so it can be exited, do the following:
Quickly press your device's Home button three times to bring up the Guided Access menu.
Enter your four digit passcode when prompted.
Tap the End button in the top left corner of the screen to end Guided Access. A message stating "Guided Access Ended" will briefly appear.
Here's the knowledgebase article of Apple: http://support.apple.com/kb/ht5509

- 43
- 4