Log every button press / interaction in an iOS App

前端 未结 2 667
萌比男神i
萌比男神i 2021-02-13 21:32

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 ideall

相关标签:
2条回答
  • 2021-02-13 21:52

    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]));
        }
    }
    
    0 讨论(0)
  • 2021-02-13 22:08

    What exactly you need is Google Analytics. Logging each user interaction is not the proper way. You can use the google analytics to track user interactions within your app.

    0 讨论(0)
提交回复
热议问题