0

My objective is to detect any touch event inside My Application on any view... (i.e Inside my application any touch event in anywhere should be detected...) I have tried it by subclassing my appDelegate Class with UIApplication but it is giving me error

how to detect idle user in iphone-sdk

how to resolve that error Or implement it in any other way...

Please Help

Thanks

Community
  • 1
  • 1
Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88

2 Answers2

1

OK, I've answered that linked question. However you may want to consider a different approach, which is using class_replaceMethod() to "swizzle" the UIView touch methods with your own implementation.

0

Here is a detector that tested ok on IOS 4 and 5. There is a caveat. If you are using gesture recognizers, you must set the global AppTouched to false when they get to state UIGestureRecognizerStateEnded.

#import <objc/runtime.h> 

Boolean AppTouched = false;     // provide a global for touch detection    

static IMP iosBeginTouch = nil; // avoid lookup every time through
static IMP iosEndedTouch = nil;
static IMP iosCanedTouch = nil;

// implement detectors for UIView
@implementation  UIView (touchesBeganDetector)
- (void)touchesBeganDetector:(NSSet *)touches withEvent:(UIEvent *)event
{
    AppTouched = true;

    if ( iosBeginTouch == nil )
        iosBeginTouch = [self methodForSelector:
                              @selector(touchesBeganDetector:withEvent:)];

    iosBeginTouch( self, @selector(touchesBegan:withEvent:), touches, event );
}
@end

@implementation  UIView (touchesEndedDetector)
- (void)touchesEndedDetector:(NSSet *)touches withEvent:(UIEvent *)event
{
    AppTouched = false;

    if ( iosEndedTouch == nil )
        iosEndedTouch = [self methodForSelector: 
                              @selector(touchesEndedDetector:withEvent:)];

    iosEndedTouch( self, @selector(touchesEnded:withEvent:), touches, event );
}
@end

@implementation  UIView (touchesCancledDetector)
- (void)touchesCancledDetector:(NSSet *)touches withEvent:(UIEvent *)event
{
    AppTouched = false;

    if ( iosCanedTouch == nil )
        iosCanedTouch = [self methodForSelector:     
                              @selector(touchesCancledDetector:withEvent:)];

    iosCanedTouch( self, @selector(touchesCancelled:withEvent:), touches, event );
}
@end

// http://stackoverflow.com/questions/1637604/method-swizzle-on-iphone-device
static void Swizzle(Class c, SEL orig, SEL repl )
{
    Method origMethod = class_getInstanceMethod(c, orig );
    Method newMethod  = class_getInstanceMethod(c, repl );

    if(class_addMethod( c, orig, method_getImplementation(newMethod),
                                 method_getTypeEncoding(newMethod)) )

        class_replaceMethod( c, repl, method_getImplementation(origMethod), 
                                      method_getTypeEncoding(origMethod) );
    else
        method_exchangeImplementations( origMethod, newMethod );
}

@interface  touchDetector : NSObject {}
- (id) init;
@end

@implementation touchDetector

- (id) init
{
     if ( ! [ super init ] )
         return nil;

    SEL rep = @selector( touchesBeganDetector:withEvent: );
    SEL orig = @selector( touchesBegan:withEvent: );
    Swizzle( [UIView class], orig, rep );

    rep = @selector( touchesEndedDetector:withEvent: );
    orig = @selector( touchesEnded:withEvent: );
    Swizzle( [UIView class], orig, rep );

    rep = @selector( touchesCancledDetector:withEvent: );
    orig = @selector( touchesCancelled:withEvent: );
    Swizzle( [UIView class], orig, rep );

    return self;
}
@end
gjpc
  • 1,428
  • 14
  • 21
  • I am using iOS 7.1 and after using this in a slightly different implementation, I can't get the source UIView to receive touchesBegan, etc to respond. I get EXC_BAD_ACCESS code=2 address=0x0 on the line iosBeginTouch( self, @selector(touchesBegan:withEvent:), touches, event ); – George L May 19 '14 at 00:33