2

In my iphone app i set delegate of my uiwebview to files owner but it is not calling uiscrollviewdelegate methods

my code is

- (void)webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"webViewDidStartLoad  ");
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
     NSLog(@"webViewDidFinishLoad  ");
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

     NSLog(@"scrollViewDidEndDecelerating  ");
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    NSLog(@"scrollViewDidScroll  ");
}

but i am getting

webViewDidStartLoad
webViewDidFinishLoad

these messages in the console.

What is wrong with me

Thanks in advance

sergio
  • 68,819
  • 11
  • 102
  • 123
Johnykutty
  • 12,091
  • 13
  • 59
  • 100
  • [This link](http://fr.ivolo.us/posts/iphone-custom-gestures-on-your-uiwebview) is exactly what you want. Or download the [custom webView files here](http://cl.ly/0K2s1m2A3u3g09032V02) – tipycalFlow Jan 03 '12 at 09:06

4 Answers4

6

UIWebView's delegate property only allows you to set the delegate for the UIWebViewDelegate protocol. (Look here for UIWebView reference.)

UIWebView is in itself a UIScrollViewDelegate, in that it receives the delegate calls from the UIScrollView it contains. Unfortunately, you have no API-access to that UIScrollView, so you cannot change its delegate; thus, it remains hard-coded to the UIWebView itself.

Now, there are several ways you can try and get those delegate messages.

One way for you to get UIScrollViewDelegate calls is subclassing UIWebView, if that works. In any case, you should be very careful, because depending on what you do you might break the UIWebView integrity and some features could stop working (possibly with some future SDK release). Furthermore, Apple discourages doing that, although it will not make your app rejected (as per many people who have reported doing that and having no problems with the App Store approval).

Another approach could be iterating through the subviews of your UIWebView until you find a UIScrollView object and then change its delegate.

UIScrollView* scrollView = nil;
for (UIView* subview in [webView subviews]) {
   if ([subview isKindOfClass:[UIScrollView class]]) {  
      scrollView = (UIScrollView*)subview;
      scrollView.delegate = <YOUR_DELEGATE_HERE>;
      break;
   }
}

I am not sure if this is reliable or may break anything, since the coupling between UIWebView and its UIScrollView child is very tight and not described anywhere, but you can have a try. All in all, I don't think this is much better than subclassing UIWebView.

Finally, a third approach is defining your own UIWindow type. This will allow you to override a few key methods (sendEvent or hitTest:event:) so that you can intercept the touches before they are dispatched at the view level. This will give you a chance to handle them and, if required, prevent them from reaching their original target.

This is the cleanest solution, although it is more work and it will require you to define a custom UIWindow where you do all the touch recognizing/dispatching.

A tutorial describing how to override sendEvent is available here.

sergio
  • 68,819
  • 11
  • 102
  • 123
  • In their `UIWebview` reference, Apple says that `The UIWebView class should not be subclassed.` – tipycalFlow Jan 02 '12 at 09:26
  • Thanks, @tipycalFlow. In any case, it must be taken into account that the OP is trying to do something that is beyond what Apple allows to do; so, in that specific case, it *might* make sense disregarding Apple suggestion as to `UIWebView` override. Of course, there is no guarantee that things will work right or that they will not break in the future. – sergio Jan 02 '12 at 09:44
  • indeed, true... Also, this won't work `[webView subviews]`. [This link](http://fr.ivolo.us/posts/iphone-custom-gestures-on-your-uiwebview) talks about how to subclass the `UIWebview` correctly. – tipycalFlow Jan 02 '12 at 09:54
3

For those that are looking after the SWIFT 3 solution:

set UIScrollViewDelegate and then here is an example:

var noticias: UIWebView!
noticias.scrollView.delegate = self
Alex Delgado
  • 984
  • 11
  • 19
2

UIWebView has it's own UIScrollView.Maybe you can try like this self.webView.scrollView.delegate = self;

无夜之星辰
  • 5,426
  • 4
  • 25
  • 48
0

the scrollView of UIWebView actually has a writeable delegate property since iOS2. It works perfectly for me to use it.