Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log every button press / interaction in an iOS App

Is there a way to catch all sorts of user interactions, but first and foremost button presses in an iOS app? I'm interested in logging these events with a time stamp and ideally with the name of the screen they appear on.

I guess the simplest way is to insert a call of a custom log function into every action called by a button. But that's too much effort.

I also thought about subclassing UIButton, but this would still require me to change every button in an existing app and would work only for buttons (not cells in a table for example).

Is there a point were I can intercept touches in general? Or a point were I specifically know a button was pressed and I've a reference to that button?

(We research usability testing of mobile apps, so we aim at a modular solution that can be easily reused and needs as little manual changes of code as possible. But any suggestions are welcome, since I realize this might not be so easy.)

like image 660
Christian Avatar asked Jul 01 '13 10:07

Christian


Video Answer


1 Answers

You can subclass UIApplication:

  • Create an UIApplication Subclass
  • override the -(BOOL)sendAction:(SEL)action to:(id)target from:(id)sender forEvent:(UIEvent *)event method, remember to call the super implementation
  • put an NSLog or other diagnostic code inside the implementation

Example, this will print a log every time an UIButton is pressed:

-(BOOL)sendAction:(SEL)action to:(id)target from:(id)sender forEvent:(UIEvent *)event
{
    if ([sender isKindOfClass:[UIButton class]])
    {
        NSLog(@"Action: %@ - %@ - %@", NSStringFromSelector(action), target, sender);
    }

    return [super sendAction:action to:target from:sender forEvent:event];
}


2013-07-08 14:46:18.270 UIApplicationSubclass[94764:c07] Action: anAction: - <ViewController: 0x76790a0> - <UIRoundedRectButton: 0x767b9b0; frame = (103 66; 73 44); opaque = NO; autoresize = TM+BM; layer = <CALayer: 0x767bad0>>
2013-07-08 14:46:27.378 UIApplicationSubclass[94764:c07] Action: anAction: - <ViewController: 0x76790a0> - <UIRoundedRectButton: 0x767b9b0; frame = (103 66; 73 44); opaque = NO; autoresize = TM+BM; layer = <CALayer: 0x767bad0>>

Moreover, to subclass UIApplication, you must change the main.m file like this (In my case the UIApplication subclass was named FLApplication, look at the third parameter of the UIApplicationMain function and the import of FLApplication.h)

#import "AppDelegate.h"
#import "FLApplication.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, NSStringFromClass([FLApplication class]), NSStringFromClass([AppDelegate class]));
    }
}
like image 109
LombaX Avatar answered Oct 14 '22 00:10

LombaX