Mobile Development 8 min read

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
iOS Interaction Blocking: Intercepting User Events at UIApplication Level

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 {
if (self.eventDisabled && (event.type == UIEventTypeTouches || event.type == UIEventTypeMotion)) {
return;
}
[super sendEvent: event];
}

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 {
self.userInteractionEnabled = NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration*NSEC_PER_SEC), dispatch_get_main_queue(), ^{
self.userInteractionEnabled = YES;
});
}

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.

mobile developmentiOSSwiftEvent HandlingObjective-CUser InteractionUIApplicationUIEvent
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login 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.