From 3ec2fe9a8d4eab00c3745b83bb6486b5da99ce6f Mon Sep 17 00:00:00 2001 From: "pouria.amini" Date: Fri, 19 Jul 2024 08:24:04 -0700 Subject: [PATCH] fix: track button clicks only on touchupinside --- .../Plugins/iOS/UIKitUserInteractions.swift | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/Sources/Amplitude/Plugins/iOS/UIKitUserInteractions.swift b/Sources/Amplitude/Plugins/iOS/UIKitUserInteractions.swift index b767386..1b90c0d 100644 --- a/Sources/Amplitude/Plugins/iOS/UIKitUserInteractions.swift +++ b/Sources/Amplitude/Plugins/iOS/UIKitUserInteractions.swift @@ -68,7 +68,7 @@ extension UIApplication { guard sendActionResult, let view = sender as? UIView, - view.amp_shouldTrack(action, for: event), + view.amp_shouldTrack(action, to: target), let actionName = NSStringFromSelector(action) .components(separatedBy: ":") .first @@ -88,7 +88,7 @@ extension UIGestureRecognizer { @objc func amp_setState(_ state: UIGestureRecognizer.State) { amp_setState(state) - guard state == .ended, let view else { return } + guard state == .ended, let view, let name, !name.isEmpty else { return } let gestureType = switch self { case is UITapGestureRecognizer: "Tap" @@ -178,37 +178,27 @@ extension NSObject { protocol ActionTrackable { var amp_title: String? { get } - func amp_shouldTrack(_ action: Selector, for event: UIEvent?) -> Bool + func amp_shouldTrack(_ action: Selector, to target: Any?) -> Bool } extension UIView: ActionTrackable { @objc var amp_title: String? { nil } - @objc func amp_shouldTrack(_ action: Selector, for event: UIEvent?) -> Bool { true } + @objc func amp_shouldTrack(_ action: Selector, to target: Any?) -> Bool { false } } extension UIButton { override var amp_title: String? { currentTitle } + + override func amp_shouldTrack(_ action: Selector, to target: Any?) -> Bool { + actions(forTarget: target, forControlEvent: .touchUpInside)?.first == action.description + } } extension UISegmentedControl { override var amp_title: String? { titleForSegment(at: selectedSegmentIndex) } } -extension UITextField { - override func amp_shouldTrack(_ action: Selector, for event: UIEvent?) -> Bool { false } -} - -extension UITextView { - override func amp_shouldTrack(_ action: Selector, for event: UIEvent?) -> Bool { false } -} - #if !os(tvOS) -extension UISlider { - override func amp_shouldTrack(_ action: Selector, for event: UIEvent?) -> Bool { - event?.allTouches?.contains { $0.phase == .ended && $0.view === self } ?? false - } -} - @available(iOS 14.0, *) extension UIColorWell { override var amp_title: String? { title }