Skip to content

Instantly share code, notes, and snippets.

@Xmoe
Created August 16, 2015 18:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xmoe/23599403bdf9718d69ff to your computer and use it in GitHub Desktop.
Save Xmoe/23599403bdf9718d69ff to your computer and use it in GitHub Desktop.
objc_util_ext.py
# coding: utf-8
from objc_util import *
NotificationCenter = ObjCClass('NSNotificationCenter').defaultCenter()
_registered_notifications = [] # keep a reference, so you can later delete them.
def import_framework(name):
fmw = ObjCClass('NSBundle').bundleWithPath_('/System/Library/Frameworks/{}.framework'.format(name))
fmw.load()
return fmw
def observer_add(notification_name,function,sender=None):
def internalFunction_(_self,_cmd,n):
function(n)
NotificationObserver = create_objc_class('NotificationObserver', methods=[internalFunction_],debug=False)
observer_instance = NotificationObserver.alloc().init()
NotificationCenter.addObserver_selector_name_object_(observer_instance, 'internalFunction:', notification_name, sender)
_registered_notifications.append(observer_instance)
return observer_instance
def observer_trigger(notification):
NotificationCenter.postNotificationName_object_(notification,None)
def observer_remove(observer):
NotificationCenter.removeObserver_(observer)
_registered_notifications.remove(observer)
def observer_remove_all():
while len(_registered_notifications) > 0:
observer_remove(_registered_notifications[-1])
def dir_print(objc_object):
print '\n'.join(dir(objc_object))
if __name__ == '__main__':
def keyboard_changed(n):
print 'Keyboard state was changed'
try:
keyboard_up_observer = observer_add('UIKeyboardDidShowNotification',keyboard_changed)
keyboard_down_observer = observer_add('UIKeyboardDidHideNotification',keyboard_changed)
while True:
pass
finally:
observer_remove(keyboard_up_observer)
observer_remove_all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment