Is there a way to "capture" all the UIButton taps inside an app?

899 Views Asked by At

I want to be able to track what buttons my users are tapping. Is there a way to "capture" or "log" all the button taps inside my app?

I was thinking about method swizzling but I really rather not get into that.

3

There are 3 best solutions below

1
On

Buttons can have multiple actions attached to them. You could always add a logging action to each of your buttons. The action method receives the sender, so you could log information about the button that was tapped.

0
On

Aspect Programming may help you. Have a look at this library :

https://github.com/steipete/Aspects

Basically you do something like :

[UIButton aspect_hookSelector:@selector(whateverSelectorYouWantToHookOn:) 
                  withOptions:AspectPositionAfter 
                   usingBlock:^(id<AspectInfo> aspectInfo) {
   NSLog(@"UIButton called");
} 
error:NULL];

Have a look on the AspectInfo for more information on the instance called.

0
On

You can subclass UIButton to add logging. The accepted answer on

objective C: Buttons created from subclass of UIButton class not working

has a good explanation on why this is one of the few instances that subclassing may be useful.

Basically UIButton inherits from UIControl and UIControl Class Reference states

Subclassing Notes You may want to extend a UIControl subclass for either of two reasons:

To observe or modify the dispatch of action messages to targets for particular events
To provide custom tracking behavior (for example, to change the highlight appearance)