Skip to content

Instantly share code, notes, and snippets.

@EmielM
Created May 8, 2014 18:31
Show Gist options
  • Save EmielM/19eb729e15745ae44958 to your computer and use it in GitHub Desktop.
Save EmielM/19eb729e15745ae44958 to your computer and use it in GitHub Desktop.
UIWebview enhancement hacks
// Continuously changing GIST of UIWebView 'tweaks' I use. Might be useful to others,
// hope Google finds this. Some of these already passed Review, but one never knows ;).
// Happy coding!
- (void)ScrollView_setContentOffset:(CGPoint)offset {
// Prevent superfluous scrolling animations (eg when toggling keyboard) by completely disabling scrolling. We achieve scrolling through inner scroll views (overflowing html elements).
}
- (id)WebBrowserView_inputAccessoryView {
// Make the keyboard accessory view (next/prev,submit buttons) optional, it really takes up to much screen estate in a normal app.
if (keybAccessory)
return [super inputAccessoryView];
else
return nil;
}
- (void)WebView_webView:(id)arg1 decidePolicyForGeolocationRequestFromOrigin:(id)arg2 frame:(id)arg3 listener:(id)arg4 {
// When using the HTML5 location API, UIWebViews's default implementation triggers an additional "file://longUrl Wants To Access Your Location" modal, directly after the App asks for permission.
SEL selector = NSSelectorFromString(@"allow");
if([arg4 respondsToSelector:selector]) {
((void (*)(id,SEL))[arg4 methodForSelector:selector])(arg4,selector);
// just invoking the selector..
NSLog(@"allowed geolocation request");
}
}
- (void) tweakWebView {
Class WebView = objc_allocateClassPair(objc_getClass("UIWebView"), "MyWebView", 0);
class_addMethod(WebView,
@selector(webView:decidePolicyForGeolocationRequestFromOrigin:frame:listener:),
[self methodForSelector: @selector(WebView_webView:decidePolicyForGeolocationRequestFromOrigin:frame:listener:)],
"v@:");
objc_registerClassPair(WebView);
object_setClass(web, WebView);
Class ScrollView = objc_allocateClassPair(objc_getClass("UIScrollView"), "MyScrollView", 0);
class_addMethod(ScrollView, @selector(setContentOffset:),
[self methodForSelector:@selector(ScrollView_setContentOffset:)], "@@:");
objc_registerClassPair(ScrollView);
object_setClass(web.scrollView, ScrollView);
Class WebBrowserView = objc_allocateClassPair(objc_getClass("UIWebBrowserView"), "MyWebBrowserView", 0);
class_addMethod(WebBrowserView, @selector(inputAccessoryView),
[self methodForSelector:@selector(WebBrowserView_inputAccessoryView)], "v@:");
objc_registerClassPair(WebBrowserView);
for (UIView *subview in web.scrollView.subviews) {
if ([NSStringFromClass([subview class]) isEqualToString:@"UIWebBrowserView"]) {
object_setClass(subview, WebBrowserView);
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment