Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
UIWebView addition to enable/disable scrolling
#import <UIKit/UIKit.h>
@interface UIWebView (Additions)
/*!
@abstract Enable/Disable the receiver from scrolling.
*/
@property (nonatomic, assign) BOOL webViewScrollEnabled;
@end
#import "UIWebView+Additions.h"
@implementation UIWebView (Additions)
- (BOOL)webViewScrollEnabled
{
// iOS 3.2 or above
// - UIWebView has inner UIScrollView
// so we can use it easily
for (id subview in self.subviews) {
if ([subview isKindOfClass: [UIScrollView class]]) {
return ((UIScrollView *)subview).scrollEnabled;
}
}
// iOS 3.1.3 or less
// - UIWebView doesn't contain UIScrollView
// - UIWebView uses custom scroller class
// we can use its method
//
// Customized the way introduced in these page
// http://d.hatena.ne.jp/nakamura001/20090520/1242837408
// http://praveenmatanam.wordpress.com/2009/04/03/how-to-disable-uiwebview-from-user-scrolling/
//
// I'm not sure the name of the getter method, probably which is "isScrollingEnabled" or "scrollingEnabled",
// so I'm trying both of them
//
// WARNING
// Apple will most likely REJECT your app if you try this hack.
// If you dare, you MUST encrypt/hide your selector name before this code and decrypt it in runtime.
// (I do not suggest how to encrypt selector names. Try it on your own risk)
//
SEL selector = @selector(isScrollingEnabled);
for (id subview in self.subviews) {
if ([subview respondsToSelector:selector]) {
NSMethodSignature *sig = [subview methodSignatureForSelector:selector];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:sig];
[invocation setTarget:subview];
[invocation setSelector:selector];
[invocation invoke];
BOOL result = YES;
[invocation getReturnValue:&result];
return result;
}
}
selector = @selector(scrollingEnabled);
for (id subview in self.subviews) {
if ([subview respondsToSelector:selector]) {
NSMethodSignature *sig = [subview methodSignatureForSelector:selector];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:sig];
[invocation setTarget:subview];
[invocation setSelector:selector];
[invocation invoke];
BOOL result = YES;
[invocation getReturnValue:&result];
return result;
}
}
// Couldn't find the current state. Maybe it's YES (the default)
return YES;
}
- (void)setWebViewScrollEnabled:(BOOL)b
{
// iOS 3.2 or above
// - UIWebView has inner UIScrollView
// so we can use it easily
for (id subview in self.subviews) {
if ([subview isKindOfClass: [UIScrollView class]]) {
((UIScrollView *)subview).scrollEnabled = b;
return;
}
}
// iOS 3.1.3 or less
// - UIWebView doesn't contain UIScrollView
// - UIWebView uses custom scroller class
// we can use its method
//
// Customized the way introduced in these page
// http://d.hatena.ne.jp/nakamura001/20090520/1242837408
// http://praveenmatanam.wordpress.com/2009/04/03/how-to-disable-uiwebview-from-user-scrolling/
//
// WARNING
// Apple will most likely REJECT your app if you try this hack.
// If you dare, you MUST encrypt your selector name before this code and decrypt it in runtime.
// (I do not suggest how to encrypt selector names. Try it on your own risk)
//
SEL selector = @selector(setScrollingEnabled:);
for (id subview in self.subviews) {
if ([subview respondsToSelector:selector]) {
NSMethodSignature *sig = [subview methodSignatureForSelector:selector];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:sig];
[invocation setTarget:subview];
[invocation setSelector:selector];
[invocation setArgument:&b atIndex:2];
[invocation invoke];
return;
}
}
}
@end

newacct commented Jul 30, 2011

[[subview class] isSubclassOfClass: [UIScrollView class]]
can be written as
[subview isKindOfClass: [UIScrollView class]]

Owner

akisute commented Jul 30, 2011

Thanks newacct, you're right and probably that's more easier to understand. I'll fix it up.

Owner

akisute commented Jul 30, 2011

Also added some warning comments. I've got my app rejected by using this code ;(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment