AppleNotificationCenter - UBogun/Xojo-iosLib GitHub Wiki

Inherits from AppleObject
Memory leak check: not yet done.
Status: completed.

This is an implementation of NSNotificationCenter, the class you can use to catch system notifications and forward them to events or custom methods.
You can even create your own custom notifications and use the notification center for distribution. I have not compared the performance against Xojo communication features.

NSNotificationCenter is a singleton object. All instances of AppleNotificationCenter you might create share the same central instance. All methods are shared methods, and you do not have to worry about addressing the DefaultCenter. Instead, simply use the methods like described below:

General information

Notifications that are registered on an object will fire the block you define as an AppleBlock. The Block must take a Notification as ptr value which it can use to build an AppleUserNotification object and examine its userDict and object properties.

Xojo integration

See above. Simply use the shared methods.
For registering notifications, take this example from the AppleWindow class:

dim result as iOSLibNotificationObject
dim VisibleBlock as new AppleBlock (addressof BecameVisibleBlock)
result = AppleNotificationCenter.AddObserverForName (kUIWindowDidBecomeVisibleNotification, id, AppleOperationQueue.MainQueue, VisibleBlock.Handle)
NotificationObjects.Value(result) = result

where BecameVisibleBlock is a hidden shared method:
dim notificationobject as new AppleNotification (notification)
dim ego as AppleWindow = AppleWindow.MakefromPtr(notificationobject.NotificationObject.id)
if ego <> nil then ego.informonshown

and informonShown raises the Shown Event on the instance.

The iOSLibNotificationObject is buffered in a dictionary to use the RemoveObserver method on deconstruction time.

Constructor

See above. There is no constructor, instead you use shared methods on a shared singleton object.

Shared properties

DefaultCenter As AppleNotificationCenter (read-only): The singleton instance.

Shared methods

AddObserverForName (NotificationName as CFStringRef, Obj as appleobject, queue as AppleOperationQueue, block as appleblock) As iOSLibNotificationObject: Adds an entry to the NotificationCenter’s dispatch table with a notification queue and a block that will be called when the notification occurs, and optional criteria: notification name and sender.
If you don't define a notification name, all notifications from obj will be used to fire the block. You can pass nil instead of a queue to use the main queue.
Returns an iOSLibNotificationObject that you can use to deregister the notification.
See the integration example above.

AddObserverForSelector (selector as ptr, NotificationName as CFStringRef, Obj as appleobject): Adds an entry to the NotificationCenter’s dispatch table with an observer, a notification selector and optional criteria: notification name and sender.
If you don’t define a notification name, all notification received from Obj will be forwarded. If you don't define an object, all notifications of the name will be forwarded.
This method returns no value because the object registers itself. Forward the object to the RemoveObserver Method on deconstruction.

PostNotification (Notification As AppleNotification): Posts an AppleNotification to the registered receiver.

PostNotification (notificationName as CFStringRef, sender as appleobject = nil): Creates a notification with a given name and sender and posts it to the receiver.

PostNotification (notificationName as CFStringRef, userInfo as AppleDictionary, sender as appleobject = nil): Creates a notification with a given name, userInfo Dictionary and sender and posts it to the receiver.

RemoveObserver (Observer As AppleObject): Call this method to remove an Observer (either an iOSLibNotificationObserver or a Object that added itself for a selector) from the Notification center’s dispatch list.

RemoveObserver (Observer As AppleObject, opt. NotificationName As CFStringRef, opt. sender as appleobject): Call this method to remove an Observer (either an iOSLibNotificationObserver or a Object that added itself for a selector) from the Notification center’s dispatch list for only a certain notification or a certain sender object.