Youngwing
Youngwing

Reputation: 577

TapDetectingWindow on WebView is not Working Properly

In my app I am using TapDetectingWindow to detect touches on a UIWebView (exactly as detailed on http://mithin.in/2009/08/26/detecting-taps-and-events-on-uiwebview-the-right-way). I used it in one View Controller as suggested with a reference in the header file and the following in the implementation file like

- (id)init {
    self = [super init];
    if (self) {
          tapDetectingWindow = (TapDetectingWindow *)[[UIApplication sharedApplication].windows objectAtIndex:0];                                               //mWindow, variable reference to TapDetectingWindow
        tapDetectingWindow.viewToObserve = webView;  
        tapDetectingWindow.controllerThatObserves = self;    

        webView = [[UIWebView alloc] init];
        [webView setFrame:CGRectMake(0, 0, 340, 1900)];
        [webView setTag:1];
        [webView addSubview:keyboardText];
        [webView setUserInteractionEnabled:NO];
        [webView setDelegate:self];
        [webView setOpaque:0.0];

       [webView loadHTMLString:htmlString baseURL:NULL];
        [self.view  addSubview:webView];

        keyboardText = [[UITextField alloc] init];
        keyboardText.autocorrectionType = UITextAutocorrectionTypeNo;   
        [keyboardText setDelegate:self];
        [self.view addSubview:keyboardText];

    }
    return self;
}

but my app is crashing at
"tapDetectingWindow.viewToObserve = webView "
with a report * -[UIWindow setViewToObserve:]: unrecognized selector sent to instance 0x4a26b90

Can Any one help me out ...

Upvotes: 0

Views: 517

Answers (2)

piam
piam

Reputation: 653

what Apurv told you is correct and work like a charm. I see you ask for alternate method to recognise gestures on a webview (I assume that it means click on UIWebView.). The answer is yes, you have to use " UIWebView delegate method ".

#pragma mark -
#pragma mark uiwebview delegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if(navigationType==UIWebViewNavigationTypeLinkClicked){
        NSURL *URL = [request URL];
        if(URL){
            [[UIApplication sharedApplication] openURL:URL];
            return NO;
        }
    }
    return YES;
}

The code above, when you click on UIWebView, it will open the link of UIWebView that you clicked. Good luck.

Upvotes: 0

Apurv
Apurv

Reputation: 17186

The problem is not in the above code. Its due to new property introduction in iOS 5.0 with name window under UIApplicationDelegate protocol.

Change your window name in appdelegate file.

In .h file

@class WebViewController;
@class TapDetectingWindow;
@interface test_touchAppDelegate : NSObject <UIApplicationDelegate>
{
  TapDetectingWindow *myWindow;
  WebViewController *viewController;
}
@property (retain, nonatomic) TapDetectingWindow *myWindow;
@property (retain, nonatomic) WebViewController *viewController;
@end

In .m file

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  self.myWindow = [[TapDetectingWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  self.viewController = [[WebViewController alloc] init]; 
  self.myWindow.rootViewController = self.viewController;
  [self.myWindow makeKeyAndVisible];
  return YES;
}

Hope this will help.

Upvotes: 4

Related Questions