0

I am new to iPhone developer,

TouchCapturingWindow.h

@interface TouchCapturingWindow : UIWindow
{ 
       NSMutableArray *views;

       @private
       UIView *touchView;
}
- (void)addViewForTouchPriority:(UIView*)view;
- (void)removeViewForTouchPriority:(UIView*)view;
@end

TouchCapturingWindow.m

 (void)addViewForTouchPriority:(UIView*)view
 {
    if ( !views ) views = [[NSMutableArray alloc] init];
    [views addObject:view];
}
- (void)removeViewForTouchPriority:(UIView*)view 
{
    if ( !views ) return;
    [views removeObject:view];
}
- (void)sendEvent:(UIEvent *)event 
 {
    UITouch *touch = [[event allTouches] anyObject];
    if (touch.phase == UITouchPhaseBegan) 
    {
        for ( UIView *view in views ) 
        {
            if ( ![view isHidden] && [view pointInside:[touch locationInView:view] withEvent:event] ) 
            {
                touchView = view;
                [touchView touchesBegan:[event allTouches] withEvent:event];
                break;
            }
        }
    }
    else if (touch.phase == UITouchPhaseMoved) 
    {
        if ( touchView ) 
        {
            [touchView touchesMoved:[event allTouches] withEvent:event];
        }
    }
    else if (touch.phase == UITouchPhaseCancelled) 
    {
        if ( touchView ) 
        {
            [touchView touchesCancelled:[event allTouches] withEvent:event];
            touchView = nil;
        }
    }
    else if (touch.phase == UITouchPhaseEnded) 
    {
        if ( touchView ) 
        {
            [touchView touchesEnded:[event allTouches] withEvent:event];
            touchView = nil;
        }
    }
    [super sendEvent:event];
}
@end

DownloadPage.m

- (void)viewDidLoad
{
    [super viewDidLoad];   
    CGRect webFrame = CGRectMake(0.0, 0.0, 500.0, 500.0);
    webView = [[UIWebView alloc] initWithFrame:webFrame];
    [webView setBackgroundColor:[UIColor greenColor]];
    NSString *urlAddress = @"http://stackoverflow.com/";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];
    [self.view addSubview:webView]; 
    NSString *t= [webView stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"];
    NSLog(@"selected text=%@",t);
}
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event 
 {
       NSLog(@"Touches began"); 
 }
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event      
 {
      NSLog(@"Touches moved"); 
 } 
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event 
 { 
      NSLog(@"Touches ended"); 
 }
- (void) touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event 
 { 
      NSLog(@"Touches cancelled"); 
 }

Any help will be appreciated.

Ravi Kumar Karunanithi
  • 2,151
  • 2
  • 19
  • 41
Krunal
  • 6,440
  • 21
  • 91
  • 155

1 Answers1

0

It is an old "problem".

The way I did it is to add a link to your html button or any element you wish .

Then implement this UIWebView delegate method (Don't forget to add UIWebViewDelegate in your h file). this methos is called when a link is pressed in the webview so you can use it for your own needs:

-(bool) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{

    //Do what ever you want here


    //prevent the web View from loading the link
    return NO;

}

BTW You can use NSURL *url = request.URL; to check the URL if you have links you want to be active or if you have more then one operation you want to implement.

shannoga
  • 19,649
  • 20
  • 104
  • 169