TapAtPoint in a subclass of UIWebView

I have subclassed UIWebView so that I can receive touch events, and also implemented this convenient method. I am curious if this will work on a real iOS device. I am not in the office, so I do not know if this is so. It seems to be working in a simulator.

- (void) tapAtPoint:(CGPoint)point
{
    id /*UIWebBrowserView*/ webBrowserView = nil;
    id webViewInternal = nil;
    object_getInstanceVariable(self, "_internal", (void **)&webViewInternal);
    object_getInstanceVariable(webViewInternal, "browserView", (void **)&webBrowserView);

    if (webBrowserView) {
        [webBrowserView tapInteractionWithLocation:point];
    }
}

Has anyone tried something like this? I will definitely find out in the morning, lol.

+5
source share
2 answers

Please try this code, it works fine here.

/* TapDetectingWindow.m */

#import "TapDetectingWindow.h"
@implementation TapDetectingWindow
@synthesize viewToObserve;
@synthesize controllerThatObserves;
- (id)initWithViewToObserver:(UIView *)view andDelegate:(id)delegate {
    if(self == [super init]) {
        self.viewToObserve = view;
        self.controllerThatObserves = delegate;
    }
    return self;
}
- (void)dealloc {
    [viewToObserve release];
    [super dealloc];
}
- (void)forwardTap:(id)touch {
    [controllerThatObserves userDidTapWebView:touch];
}
- (void)sendEvent:(UIEvent *)event {
    [super sendEvent:event];
    if (viewToObserve == nil || controllerThatObserves == nil)
        return;
    NSSet *touches = [event allTouches];
    if (touches.count != 1)
        return;
    UITouch *touch = touches.anyObject;
    if (touch.phase != UITouchPhaseEnded)
        return;
    if ([touch.view isDescendantOfView:viewToObserve] == NO)
        return;
    CGPoint tapPoint = [touch locationInView:viewToObserve];
    NSLog(@"TapPoint = %f, %f", tapPoint.x, tapPoint.y);
    NSArray *pointArray = [NSArray arrayWithObjects:[NSString stringWithFormat:@"%f", tapPoint.x],
    [NSString stringWithFormat:@"%f", tapPoint.y], nil];
    if (touch.tapCount == 1) {
        [self performSelector:@selector(forwardTapwithObject:pointArray afterDelay:0.5];
    }
    else if (touch.tapCount > 1) {
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(forwardTap   object:pointArray];
    }
}
@end


/* WebViewController.h */

@interface WebViewController : UIViewController<TapDetectingWindowDelegate> {
    IBOutlet UIWebView *mHtmlViewer; 
    TapDetectingWindow *mWindow;
}

/* WebViewController.m */

- (void)viewDidLoad {
    [super viewDidLoad];
    mWindow = (TapDetectingWindow *)[[UIApplication sharedApplication].windows objectAtIndex:0];
    mWindow.viewToObserve = mHtmlViewer;
    mWindow.controllerThatObserves = self;
}

- (void)userDidTapWebView:(id)tapPoint
{
    NSLog(@"TapPoint = %f, %f", tapPoint.x, tapPoint.y);
}

Thanks, let me know if you have any problems.

0
source

short answer: Yes, I tried something like this in the same way, and it works on real devices (tested with iOS 6).

ARC version of your method:

- (void) tapAtPoint:(CGPoint)point
{
    Ivar internalWebViewIvar = class_getInstanceVariable([self class], "_internal");
    id internalWebView = object_getIvar(self, internalWebViewIvar);

    Ivar browserViewIvar = class_getInstanceVariable(object_getClass(internalWebView), "browserView");
    id browserView = object_getIvar(internalWebView, browserViewIvar);

    if (browserView) {
        [browserView performSelector:@selector(tapInteractionWithLocation:) withObject:[NSValue valueWithCGPoint:point]];
    }
}
0
source

All Articles