tags:

views:

87

answers:

1

I'm interested in capturing UI changes in my application programmatically and thought that the UIAccessibility protocol may help. I've found how to post UIAccessibilityLayoutChangedNotification and UIAccessibilityScreenChangedNotification but I'm not sure how to register to receive these notifications.

I've tried using NSNotificationCenter, but the name param expects a string, while the two notifications above are of the type UIAccesibilityNotifications which is an int.

Any idea how to register for these notifications?

Thanks!

+2  A: 

That's a great question! Unfortunately you cannot receive these "notifications" without affecting normal behavior. (i.e. "no you can't")


If you disassemble UIKit, you'll find UIAccessibilityPostNotification is implemented like this:

static void (*__UIAccessibilityBroadcastCallback)(UIAccessibilityNotifications notification, id argument);
void UIAccessibilityPostNotification(UIAccessibilityNotifications notification, id argument) {
    __UIAccessibilityBroadcastCallback (notification, argument);
}

That means these accessibility "notifications" aren't any normal notifications. Rather, they are just parameters to an internal callback function. How the callback function is implemented depends on the accessibility bundle you're using.

You can replace the callback function with your own using the undocumented API _UIAccessibilitySetBroadcastCallback:

void _UIAccessibilitySetBroadcastCallback(void (*replacement)(UIAccessibilityNotifications notification, id argument)) {
   __UIAccessibilityBroadcastCallback = replacement;
}

However, there isn't a corresponding "get" function (not even private), so once you set it, the original listeners cannot be notified again.

KennyTM
Thanks for the detailed explanation. Just a few followup questions:- is the _UIAccessibilitySetBroadcastCallback an instance method of UIAccessibilityPostNotification? If it is, does this mean that you can only override the callback for your own custom notifications, rather than notifications that may be implemented in UIWebView or some other such view.-I'm a little unclear how to modify a private framework. I wanted to just add this method signature to UIAccessibility.h in UIKit but I can't modify this file. Is there an easier way to access this method?
MobileDev852
@Mobile: (1) `_UIAccessibilitySetBroadcastCallback` is a C function. (2) Yes you can only override. And be overridden. But no the callback is implemented by the accessibility bundles. The `UIWebView` only post notifications. (3) You can put the function prototype anywhere. (4) I don't recommend you mess with this, jailbroken or not.
KennyTM