Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Testing if the app is running in the background
-
I am trying to write a program which if at any point, is put into the iOS background, resets back to the beginning. I have only a basic level of python experience, but I suspect that the answer involves threading, and creating a new thread that constantly checks if it is in the background. If however there is a way a command can be run whenever the program is put in the background, I would be happy to know how
-
from objc_util import * from ctypes import * import dialogs def willResignActive_(_self,_sel,_notification): print('entering background') #do stuff here MyBGDetector=create_objc_class('MyBGDetector',methods=[willResignActive_]) d=MyBGDetector.alloc().init() UIApplicationWillResignActiveNotification=ObjCInstance(c_void_p.in_dll(c,'UIApplicationWillResignActiveNotification')) NSNotificationCenter=ObjCClass('NSNotificationCenter') NSNotificationCenter.defaultCenter().addObserver_selector_name_object_(d, sel('willResignActive:'), UIApplicationWillResignActiveNotification, None)
-
@JonB Thank you so much. If anyone, or Jon, would be able to explain exactly how this works, it would be greatly appreciated.
-
IOS applications can register to recieve "notifications" of certain system events.Since pythonista has a bridge to objectivec, it is possible to tap into those events, and register a python function to be called when the event occurs.
This code probably needs to handle cleared globals, and multiple runs ("deregister" the object), etc but would work for the simple use case presented.