Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide shortcut keyboard bar for UIWebView in iOS 9

I'm developing PhoneGap application for iOS and I need to get rid of new iOS 9 shortcut bar. Now I'm doing the following in the - (void)viewDidLoad method

if ([self.webView respondsToSelector:@selector(inputAssistantItem)]) {
    UITextInputAssistantItem *inputAssistantItem = [self.webView inputAssistantItem];
    inputAssistantItem.leadingBarButtonGroups = @[];
    inputAssistantItem.trailingBarButtonGroups = @[];
}

This hides undo/redo and copy/paste buttons but shortcut bar still presented on keyboard and has navigation buttons on it.

enter image description here

How can I get rid of shortcut bar completely.

Thanks for your help!


*** UPDATE 1 ***

My full working code is below. Hope this can help someone (thanks to @Clement reply)

#import <objc/runtime.h>

- (void) hideKeyboardShortcutBar: (UIView *)view
{
    for (UIView *sub in view.subviews) {
        [self hideKeyboardShortcutBar:sub];
        if ([NSStringFromClass([sub class]) isEqualToString:@"UIWebBrowserView"]) {

            Method method = class_getInstanceMethod(sub.class, @selector(inputAccessoryView));
            IMP newImp = imp_implementationWithBlock(^(id _s) {
                if ([sub respondsToSelector:@selector(inputAssistantItem)]) {
                    UITextInputAssistantItem *inputAssistantItem = [sub inputAssistantItem];
                    inputAssistantItem.leadingBarButtonGroups = @[];
                    inputAssistantItem.trailingBarButtonGroups = @[];
                }
                return nil;
            });
            method_setImplementation(method, newImp);

        }
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self hideKeyboardShortcutBar:self.webView];
}

This trick will hide undo/redo and navigation buttons. But auto-prediction text still will be shown on keyboard. To hide shortcut bar completely add html attributes to your input element

<input autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" />

P.S My app is on the review now. Still don't know if Apple will approve this.


*** UPDATE 2 ***

My app was approved by Apple

like image 210
BorisR Avatar asked Sep 23 '15 15:09

BorisR


1 Answers

Using method swiziling we can remove the keyboard shortcut bar (only works with ObjC).

 - (void)hideKeyboardShortcutBar
    {
        Class webBrowserClass = NSClassFromString(@"UIWebBrowserView");
        Method method = class_getInstanceMethod(webBrowserClass, @selector(inputAccessoryView));

        IMP newImp = imp_implementationWithBlock(^(id _s) {
            if ([self.webView respondsToSelector:@selector(inputAssistantItem)]) {
                UITextInputAssistantItem *inputAssistantItem = [self.webView inputAssistantItem];
                inputAssistantItem.leadingBarButtonGroups = @[];
                inputAssistantItem.trailingBarButtonGroups = @[];
            }
            return nil;
        });

        method_setImplementation(method, newImp);
    }

inputAccessoryView : This property is typically used to attach an accessory view to the system-supplied keyboard that is presented for UITextField and UITextView objects.

So the new implementation block will be fired every time the keyboard pops up.

UPDATE

To remove the accessory view from WKWebView use WKContentView instead of UIWebBrowserView

like image 110
Clement Prem Avatar answered Jan 02 '23 13:01

Clement Prem