10

I am trying to force my app to automatically enter the "Suspended" state programmatically, so I can do testing on Core Bluetooth restoration. I have tried calling

[[UIApplication sharedApplication] performSelector:@selector(suspend)];

but this merely sends it into the background. Am I doing it wrong? Is there a better way? Or is it impossible?

I would like to remind everyone exactly what it means to have an app be suspended, as there is always confusion on the terminology: Link to state diagram of iPhone apps
(source: apple.com)

An app in the background will get indefinitely suspended if the system needs memory, but Core Bluetooth's restoration can send an app back into the background state temporarily.

Community
  • 1
  • 1
Jegus
  • 165
  • 1
  • 8
  • This might be of some help: [Automating UI Testing](https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/UsingtheAutomationInstrument/UsingtheAutomationInstrument.html) See the Testing for Multitasking at the bottom. It's not done from the app code itself but rather from instruments script. But it's for emulating home-button press only. – Rok Jarc Nov 15 '13 at 21:32
  • 1
    I had not tested this - it might work: http://stackoverflow.com/a/13525245/653513 – Rok Jarc Nov 15 '13 at 21:49
  • 1
    UIApplication *app = [UIApplication sharedApplication]; [app performSelector:@selector(suspend)]; – SleepNot Jun 16 '14 at 09:35
  • Did you find a way to do it programmatically? (without opening dummy 500MB app?) – Vladimir Kofman Jan 20 '16 at 09:52

1 Answers1

6

I don't know how to do this programmatically, but what I do is to press the home button, then launch a number of heavyweight apps like Safari and graphics-intensive games. The memory pressure causes the system to terminate your app pretty quickly. Core Bluetooth should then initiate the restore process and re-launch your app when it detects activity from a peripheral you are connected to/have asked to connect to.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • 3
    Haha, that's a good idea. If there's no way to do it programmatically, maybe I'll make an app that purposefully uses up as much memory as possible. – Jegus Nov 15 '13 at 21:46
  • 1
    I made a nice 500 MB application, but it's still pretty unreliable. After opening every app my phone has, including the 500 MB one, I've only managed to get the app I'm trying to test to suspend once. – Jegus Nov 15 '13 at 22:30