如何在IOS中记录每个用户的交互

如何在IOS中记录每个用户的交互,ios,objective-c,Ios,Objective C,我想通过NSLog在IOS上记录每个用户的交互, 然后可能添加文件名、方法名和行号 通常我希望看到这样的情况: UI Front-most view controller: MYViewController (no title) Interaction Action [MyViewController showMenu] by sender UIBarButtonItem 这是否可能使用Objective-C。我不想更改每个文件,而是希望调用一些全局方法。我更愿意在ApplicationLeg

我想通过NSLog在IOS上记录每个用户的交互, 然后可能添加文件名、方法名和行号

通常我希望看到这样的情况:

UI Front-most view controller: MYViewController (no title)
Interaction Action [MyViewController showMenu] by sender UIBarButtonItem

这是否可能使用Objective-C。我不想更改每个文件,而是希望调用一些全局方法。我更愿意在ApplicationLegate类中执行此操作

有些东西你理解错了
UIBarButtonItem
从来都不是发送方,因为它的类是
NSObject
而不是
UIView
UIBarButtonItem
无法接收触摸,当我们单击一个条形项目时,它接收触摸是一个
UINavigationButton
(私有类)

如果您想在用户单击视图时记录每个用户的交互,我有一个解决方案

  • 创建
    UIWindow
    的子类(调用CustomWindow)
  • 重写
    ui窗口的
    sendEvent
    方法以捕获用户交互,检查并记录其中的交互
  • didfishlaunchingwithoptions
    方法的
    AppDelegate
    内部,用类型为
    CustomWindow
    的窗口替换默认的
    window
这是我的
CustomWindow
记录用户与
UIControl
类的控制事件
UIControlEventTouchUpInSide
的交互

@implementation CustomWindow

- (void)sendEvent:(UIEvent *)event {
  UITouch *touch = [[event allTouches] anyObject];

  switch ([touch phase]) {
    case UITouchPhaseBegan:
    {
      NSLog(@"Touch Began");
    }
      break;
    case UITouchPhaseMoved:
      NSLog(@"Touch Move");
      break;
    case UITouchPhaseEnded:
    {
      NSLog(@"Touch End");
      // Check if touch ends inside view (UIControlEventTouchUpInSide)
      if (CGRectContainsPoint(CGRectMake(0, 0, touch.view.frame.size.width, touch.view.frame.size.height), [touch locationInView:touch.view])) {

        // Only log info of UIControl objects
        if ([touch.view isKindOfClass:[UIControl class]]) {
          [self logViewInfo:(UIControl *)touch.view];
        }
      }
    }
      break;
    case UITouchPhaseCancelled:
      NSLog(@"Touch Cancelled");
      break;
    default:
      break;
  }
  [super sendEvent:event];
}

- (void)logViewInfo:(UIControl *)clickedButton {
  NSString *frontMostViewcontrollerClassName = NSStringFromClass(self.topViewController.class);
  NSString *targetClassName = NSStringFromClass([[clickedButton.allTargets anyObject] class]);
  NSString *selectorName =[[clickedButton actionsForTarget:[clickedButton.allTargets anyObject] forControlEvent:UIControlEventTouchUpInside] firstObject];
  NSString *senderClassName = NSStringFromClass(clickedButton.class);

  NSLog(@"UI Front-most view controller: %@\nInteraction Action [%@ %@] by sender %@ control event UIControlEventTouchUpInSide", frontMostViewcontrollerClassName, targetClassName, selectorName, senderClassName);
}

- (UIViewController*)topViewController {
  return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}

- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)viewController {
  if ([viewController isKindOfClass:[UITabBarController class]]) {
    UITabBarController* tabBarController = (UITabBarController*)viewController;
    return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
  } else if ([viewController isKindOfClass:[UINavigationController class]]) {
    UINavigationController* navContObj = (UINavigationController*)viewController;
    return [self topViewControllerWithRootViewController:navContObj.visibleViewController];
  } else if (viewController.presentedViewController && !viewController.presentedViewController.isBeingDismissed) {
    UIViewController* presentedViewController = viewController.presentedViewController;
    return [self topViewControllerWithRootViewController:presentedViewController];
  }
  else {
    for (UIView *view in [viewController.view subviews])
    {
      id subViewController = [view nextResponder];
      if ( subViewController && [subViewController isKindOfClass:[UIViewController class]])
      {
        if ([(UIViewController *)subViewController presentedViewController]  && ![subViewController presentedViewController].isBeingDismissed) {
          return [self topViewControllerWithRootViewController:[(UIViewController *)subViewController presentedViewController]];
        }
      }
    }
    return viewController;
  }
}

有关更多详细信息,请查看我的演示回购

Hi@trungduc,谢谢您的帮助-我很快就会尝试。