Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Pranit-Harekar/87dffe9e20897c409e3afec5407cc7a7 to your computer and use it in GitHub Desktop.
Save Pranit-Harekar/87dffe9e20897c409e3afec5407cc7a7 to your computer and use it in GitHub Desktop.
Add keyboardRequiresUserInteraction to WKWebViews iOS 11.3
import Foundation
import WebKit
typealias ClosureType = @convention(c) (Any, Selector, UnsafeRawPointer, Bool, Bool, Bool, Any?) -> Void
extension WKWebView{
var keyboardDisplayRequiresUserAction: Bool? {
get {
return self.keyboardDisplayRequiresUserAction
}
set {
self.setKeyboardRequiresUserInteraction(newValue ?? true)
}
}
func setKeyboardRequiresUserInteraction( _ value: Bool) {
if #available(iOS 11.3, *) {
let WKContentView: AnyClass = NSClassFromString("WKContentView")!
let sel: Selector =
sel_getUid("_startAssistingNode:userIsInteracting:blurPreviousNode:changingActivityState:userObject:")
let method = class_getInstanceMethod(WKContentView, sel)
let originalImp: IMP = method_getImplementation(method)
let original: ClosureType = unsafeBitCast(originalImp, to: ClosureType.self)
let block : @convention(block) (Any, UnsafeRawPointer, Bool, Bool, Bool, Any?) -> Void = { (me, arg0, arg1, arg2, arg3, arg4) in
original(me, sel, arg0, !value, arg2, arg3, arg4)
}
let imp: IMP = imp_implementationWithBlock(block)
method_setImplementation(method, imp)
}
}
}
@devravenio
Copy link

it doesn't work for me
in ios9, keybaord appear and then dismiss automatically.
in ios11, keyboard doesn't appear.

@Pranit-Harekar
Copy link
Author

Pranit-Harekar commented May 4, 2018

Make sure your HTML text field has AutoFocus = true @devravenio

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