15

I have an iPhone app that I need to send to the background automatically. The app is defined with the VOIP key in its background modes so it should continue running when in background. I specifically need the app to keep running so calling exit(0) is no good.

The app will not be distributed via app store so using a private API is ok.

I have read about UIApplication terminate and UIApplication terminateWithSuccess but they don't seem to be available anymore

Cœur
  • 37,241
  • 25
  • 195
  • 267
gheese
  • 1,170
  • 1
  • 11
  • 17
  • 1
    Why would like to send to app to background!?!?!? The users starts the app after which the user will send the app to background. There is no need to send the app the background your self. Also adding the `VOIP` key to background modes will keep the app running in background and start if the device gest reset. – rckoenes Dec 21 '12 at 11:18
  • Its a desired behaviour requirement to send to background automatically (we can do it on Android), Also I know all about how VOIP works thats not the problem – gheese Dec 21 '12 at 11:20
  • It's not possible, your requirements are not compatible with the iOS system. Only user are allowed to background or close app by pressing the home button. – rckoenes Dec 21 '12 at 11:24
  • I had a feeling that was the case - just wondered if there was a private method I could use – gheese Dec 21 '12 at 11:25
  • Well there might be, but then you would definitely not get the app in the App Store. Also I'm not aware of such a method. – rckoenes Dec 21 '12 at 11:28
  • Thanks - Distribution will be via enterprise method - so app store submission will not be required – gheese Dec 21 '12 at 11:32
  • But why do you want this? exit(0); will indeed terminate the app but background? Don't think there is any... – Ron Dec 21 '12 at 11:45
  • Already answered quite well here: http://stackoverflow.com/questions/5360846/suspend-the-application – Alex Shaffer Dec 21 '12 at 14:15

3 Answers3

14

Already answered quite well here:

Suspend the application

As that poster wrote:

Quitting your application or sending it to the background programmatically is a violation of the [iOS Human Interface Guidelines][1], which usually doesn't bode well for getting through the review process:

Don’t Quit Programmatically

Never quit an iOS application programmatically because people tend to interpret this as a crash. However, if external circumstances prevent your application from functioning as intended, you need to tell your users about the situation and explain what they can do about it. Depending on how severe the application malfunction is, you have two choices.

Display an attractive screen that describes the problem and suggests a correction. A screen provides feedback that reassures users that there’s nothing wrong with your application. It puts users in control, letting them decide whether they want to take corrective action and continue using your application or press the Home button and open a different application

If only some of your application's features are not working, display either a screen or an alert when people activate the feature. Display the alert only when people try to access the feature that isn’t functioning.

Community
  • 1
  • 1
Alex Shaffer
  • 646
  • 6
  • 9
  • Thanks - I had already investigated the suspend route and found it was unsuitable as it did just that i.e. put app in suspended state , I need some background processing to continue. Any way it just confirms that it is not possible – gheese Dec 24 '12 at 09:42
9

In Swift 3 Use below code, working charm

  DispatchQueue.main.asyncAfter(deadline: .now()) {
          UIApplication.shared.perform(#selector(NSXPCConnection.suspend))
      }
Arvind Kumar
  • 2,371
  • 1
  • 18
  • 25
  • 2
    Would this pass the app review process? – Isuru Oct 25 '17 at 10:36
  • I've seen this example in the following app: https://apps.apple.com/ee/app/uzmi-ra%C4%8Dun-i-pobedi/id6444217874 My guess - Apple didn't test the button. But there is a "Cancel button" on the main screen, that suspends the app, I believe using the code above. – Nebojsa Nadj Apr 10 '23 at 19:27
3

While I agree with the other answer that you "shouldn't" exit programatically. There is a way to exit programatically.

*disclaimer - You shouldn't do this.

exit(0);

There is no way to put the application into the background without pressing the home button. If there is, you might want to add the jailbreak flag to your question and ask them.

For more, check this duplicate question, Proper way to exit application.

Community
  • 1
  • 1
Bill Burgess
  • 14,054
  • 6
  • 49
  • 86
  • Thanks for the confirmation, I had suspected it wasn't possible to background an app in code – gheese Dec 24 '12 at 09:42