Forum Archive

Effects of iPhone home button to app

donnieh

If I have some simple code that displays "hello world" in a label with a button push, we know that that text will remain there. Normally if you press the home button and then come back to the app the text should still be there. However, I am finding when running the app in Xcode that when I return the text is cleared. Basically the app runs from a fresh start when i return.

Why does this do this and how do I fix it? I have written apps in Xcode using Objective-C and it automatically remembers the app state when I return.

omz

The Xcode template checks if the script is still running when you press the home button – if it isn't, it basically calls exit(0). The intention is to make restarting a script easier and to save some memory. If you don't want that, you could put something like time.sleep(10**7) at the end of your script.

Tizzy

Just an interesting side question - how expensive, cpu-wise, is a running script while it's "sleeping?"

omz

@donnieh I have to correct myself. What I described above used to be the case, but I've changed it in the 1.6-based template that you're probably using.

You can change the behavior of the home button there by editing the "Info.plist" file that you'll find in the project. Simply select "NO" for the "Application does not run in background" setting (or delete the entire row, which is equivalent). Depending on your Xcode settings, you may see "UIApplicationExitsOnSuspend" as the setting name.

donnieh

Great! Thank you! I have to say that almost anyone making an iPhone app with Pythonista would run into this and would desire a solution.

donnieh

The app will lose its view state as well if the iOS device is rebooted. Is there a solution just as easy (allow view state to be resumed) when the device is rebooted? Maybe this is where the data should be saved to a file???