force exit to Home screen
I'm looking for some method to force exit pythonista App and return to Home screen.
It seems that there's no such method existed?
yesterday I found that sometimes the App crashed and it suddenly exited and retuned to Home screen.
since this crash always works as expected, maybe this's the method I'm looking for?
but I wonder if this'd do any harm to my iPhone if I intentionally execute such code to force crash.?
If you really want to, you can use
os.abort()to crash the app on purpose. Be aware that this is still a crash and not a clean exit, so some things might not be saved, for example modified files in the editor, or files
opened in Python.
I'm not entirely sure if
os.abortwill generate a crash report that might get sent to Apple.
os._exit(0)might be a slightly cleaner alternative, though the same caveats about files possibly not being saved etc. apply (usually not that big of a deal because the editor automatically saves before running any script).
Just curious, why do you want to do this?
thank you all
@zrzka I'm thinking of adding script to 3D touch shortcut to quickly execute some task without remaining in the code App after task done.
when I'm a code developer, I may want to execute a script and then go to code editor to check error or add features.
after code is done, I just want to execute it and then leave, do not need to enter code editor again.
Does the script produces any output? IOW what it does? I'm asking because it's not a good practice on iOS to kill apps in anyway. And maybe you can end up somewhere else by opening URL (some other app URL scheme, ...), ...?
@shtek I use the Launcher app, as widget, to launch my scripts and at the end of my scripts, I use
which displays the home screen
@zrzka I want to exit after it produce any output, and exit on demand. I know it's not the usual ios way, but why not, if I can? I'm sure I'm not the only one who wants to do so. search this forum and I found there're some posts talking about this.
@cvp are you serious? nothing happened after that url opened. you have an App named 'crash'?
oh I see, I don't have a launch App :(
you see, there's a URL for crash, because someone needs it. I think that's os._exit also? I'm not going anywhere but my Home screen......
Use case...running Pythonista script as part of Workflow from widget. I’d like to run the WF which calls the script and then returns to home. Os.abort seems like overkill. Perhaps there could be a softer way to return? ObjC?
I don't know for certain, but it's extremely unlikely that there are any ObjC APIs for exiting the app. Apple decided that apps shouldn't exit to the home screen by themselves, so I don't think they will provide APIs for it, other than standard POSIX functions like
abort. Pythonista is also not designed to be terminated from Python code (it intentionally blocks
SystemExitfrom terminating the app), so no matter what method you use, it's not going to be a clean shutdown.
There's no one for certain. People do use exit, exit on main thread (NSThread), ... But all these things lead to the crash report for Ole. It's discouraged, you can lose data, ... because app delegate is not going to receive willTerminate and app can't gracefully exit.
UIApplication.sharedApplication().suspendReturningToLastApp_(True) #or UIApplication.sharedApplication().suspend()
either seems to return to the home screen, placing pythonista in the background. You could call console.clear() first, and maybe clear globals if you want the nextblaunch to be "fresh"
Aaargh, didn't share this, because I'm against it and I don't get but why not, if I can? as an argument :) Apple's HIG Never quit an iOS application programmatically because people tend to interpret this as a crash & People, not applications, should initiate and control actions.
Anyway, it's not public API. If you're going to do this for your own purpose, feel free, but please don't include this in shared scripts / modules / ..., because people can be confused about weird behavior.
Just to clarify other responses about
abort, answer is in the QA 1561:
Do not call the
exitfunction. Applications calling
exitwill appear to the user to have crashed, rather than performing a graceful termination and animating back to the Home screen.
If during development or testing it is necessary to terminate your application, the
assertmacro is recommended.
I appreciate the reminder that Apple views os.crash as a no-no (for non-crash uses) and that users would find it confusing since apps don't just return to the homescreen normally. However, The use case I mentioned makes sense to me and I have no need to publish the script (lest someone think that Pythonista had crashed).