Skip to content
iOS Interaction Blocking: Intercepting User Events at UIApplication Level

iOS Interaction Blocking: Intercepting User Events at UIApplication Level

To globally block all user interactions—including touches, motion, remote control, and press events—developers can subclass UIApplication and override its sendEvent: method, providing a disableUserInteraction: API with timed re‑enabling and a counter to safely manage concurrent disable requests.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products

Many application developers encounter the need to block user interactions when processing sensitive core tasks or executing animations. This ensures priority completion of critical operations without external interference that could cause serious issues.

The article explains common solutions: using boolean flags to track task execution status, or setting UIView 's userInteractionEnabled property to false. However, these approaches have limitations—they only intercept touch events and cannot handle non-touch events like accelerometer or camera events.

The core solution involves understanding UIApplication 's sendEvent method, which is the top-level interaction mechanism in iOS UIKit. UIEvent supports multiple event types including UIEventTypeTouches, UIEventTypeMotion, UIEventTypeRemoteControl, UIEventTypePresses, UIEventTypeScroll, UIEventTypeHover, and UIEventTypeTransform.

To intercept sendEvent, developers can create a custom UIApplication subclass (e.g., MyApplication) and register it in the main function or via Info.plist's "Principal class" key. The subclass can override sendEvent: to filter events:

- (void)sendEvent: (UIEvent*)event {<br/>    if (self.eventDisabled && (event.type == UIEventTypeTouches || event.type == UIEventTypeMotion)) {<br/>        return;<br/>    }<br/>    <br/>    [super sendEvent: event];<br/>}

The article proposes a more robust interface design with a disableUserInteraction: method that includes automatic re-enablement after a specified duration, preventing the UI from becoming permanently unresponsive:

- (void)disableUserInteraction: (NSTimeInterval)duration {<br/>    self.userInteractionEnabled = NO;<br/>    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration*NSEC_PER_SEC), dispatch_get_main_queue(), ^{<br/>        self.userInteractionEnabled = YES;<br/>    });<br/>}

Usage:

[UIApplication.sharedApplication disableUserInteraction: 0.3];

The article also suggests further optimization using a counter mechanism to handle multiple concurrent disable requests, ensuring the longest duration request is respected.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

mobile developmentiOSSWIFTevent handlingObjective-C++user interactionUIApplicationUIEvent
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.