gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:39:48 24 4
gpt4 key购买 nike

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

通常我希望看到这样的东西:

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

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

最佳答案

有些地方你理解错了。 UIBarButtonItem never 是发件人,因为它的类是 NSObject不是UIView . UIBarButtonItem无法接收触摸,当我们点击一​​个条形项时接收触摸是一个UINavigationButton (私有(private)类(class))。

如果您想在用户点击 View 时记录每个用户交互,我有适合您的解决方案。

  • 创建 UIWindow 的子类(调用 CustomWindow)。
  • 覆盖 sendEvent UIWindow的方法|捕捉用户交互,检查并记录其中的交互。
  • 内部didFinishLaunchingWithOptions AppDelegate的方法| , 替换默认 window带有窗口的类型为 CustomWindow .

这是我的 CustomWindow记录用户与控件事件的交互 UIControlEventTouchUpInSideUIControl类。

@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;
}
}

有关更多详细信息,您可以查看我的演示库 here

关于ios - 如何记录 IOS 中的每个用户交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47238366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com