2017-11-11 93 views
1

我想通過NSLog記錄每個用戶在IOS上的交互,然後可能添加文件名,方法名稱和行號。如何記錄IOS中的每個用戶交互

通常我希望看到這樣的事情:

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

這可能使用的Objective-C。我不想更改每個文件,但寧願有一些全局方法被調用。我更喜歡在ApplicationDelegate類中執行此操作。

+0

嗨@trungduc,感謝您的幫助 - 我將盡快嘗試它。 – Wayne

回答

0

你有什麼理解錯誤。 UIBarButtonItem從不是發件人,因爲它的類是NSObject而不是UIViewUIBarButtonItem不能接收觸摸,當我們點擊一​​個酒吧項目時接收觸摸是一個UINavigationButton(私人類)。

如果您希望在用戶點擊視圖時記錄每次用戶交互,我爲您提供了一個解決方案。

  • 創建一個UIWindow(調用CustomWindow)的子類。
  • 覆蓋sendEvent方法UIWindow捕捉用戶交互,檢查並記錄裏面的交互。
  • didFinishLaunchingWithOptionsAppDelegate的方法,用一個窗口替換默認window類型爲CustomWindow

這是我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; 
    } 
} 

更多的細節,你可以檢查我的演示回購here

相關問題