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.
Run function on minimize
-
Is it possible to run a certain function when Pythonista script minimize? Basically when home button is pressed or when a notification is pressed an user leaves Pythonista.
-
it ought to be possible to swizzle applicationDidEnterBackground_ in the app delegate, though i didnt have success that way. instead, we can register an observer for UIApplicationDidEnterBackgroundNotification.
Here
https://gist.github.com/9b81f08a736421c977510e830f2d459a
is an all purpose way to use the ios notification center-- you can register a callback for any notification, including all notifications, or inclusive or exclusive filtering. The example code registers a callback for background notification, which simply prints the notification, but you can do whatever you want here.one could also add notifications for
UIApplicationWillTerminateNotification (which is actual terminination, not just background)
or
UIApplicationWillEnterForegroundNotification
to do something upon return from bg (but not termination!) -
Thank you very much for your answer! So it is possible to know if a call set my application to the background and do something? Is this possible from get call notification?
-
@JonB Perhaps is applicationDidEnterBackground never called as explained here
The reason why applicationDidEnterBackground: and applicationDidEnterForeground: are never called is because these methods are used in joint with Application does not run in background this option can be found in your ***-info.plist. If this option is set to YES than your app will never call these methods, because these when you press the home button with an app that has set the option to YES the instance of the app that is running will get terminated So everytime you press the home button and then select the app icon a new instance is being created so it is using applicationWillTerminate:.
-
@panayiotis95 It seems that when you answer to a call, your current app is not set to background but in pause.
read here -
Use notifications
UIApplicationWillResignActiveNotification UIApplicationDidBecomeActiveNotification
-
@panayiotis95 Run this little script and test with
- incoming call
- tapping on notification of incoming mail
- select another app in apps list (swipe 5 digits up)
- split view with Pythonista and another app
Based on marvelous script of @JonB , of course
# coding: utf-8 import ui import os import sys from objc_util import * import console from functools import partial import datetime NSNotificationCenter=ObjCClass('NSNotificationCenter') class GlobalNotificationObserver(object): def __init__(self,view): self.center=NSNotificationCenter.defaultCenter() self._observer=None self._blk=None self.view = view def _block(self,_cmd,notification): try: name=str(ObjCInstance(notification).name()) if 'active' in name.lower(): # UIApplicationWillResignActiveNotification # UIApplicationDidBecomeActiveNotification l = str(datetime.datetime.now()) + ' ' + name self.view['TextView'].text = self.view['TextView'].text + '\n' + l except Exception as e: print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) def start(self): if self._observer: raise Exception('observer already started') self._blk=ObjCBlock(self._block, restype=None, argtypes=[c_void_p,c_void_p]) self._observer = \ self.center.addObserverForName_object_queue_usingBlock_(None,None,None, self._blk) retain_global(self) import weakref def on_die(killed_ref): killed_ref.stop() def stop(self): import objc_util if self._observer: self.center.removeObserver_(self._observer) self._observer=None release_global(self) class MyView(ui.View): def __init__(self): self.frame = (0,0,600,600) tv = ui.TextView(name = 'TextView') tv.frame = self.frame self.add_subview(tv) self.g=GlobalNotificationObserver(self) self.g.start() def will_close(self): print(self['TextView'].text) self.g.stop() if __name__=='__main__': v = MyView() v.present('sheet')
-
Thank you very much @cvp works like charm!!