在UIWebView中,有一个属性keyboardDisplayRequiresUserAction,设置为NO时就可以在页面刚加载时直接弹出键盘;
在WKWebView中,是没有这个属性的,如果要实现类似的功能,就必须替换WKWebView中相应的方法,代码如下:
```objective-c
static void (originalIMP)(id self, SEL _cmd, void arg0, BOOL arg1, BOOL arg2, id arg3) = NULL;
void interceptIMP (id self, SEL _cmd, void* arg0, BOOL arg1, BOOL arg2, id arg3) {
originalIMP(self, _cmd, arg0, TRUE, arg2, arg3);
}
void setWkWebViewShowKeybord() {
Class cls = NSClassFromString(@"WKContentView");
SEL originalSelector = NSSelectorFromString(@"_startAssistingNode:userIsInteracting:blurPreviousNode:userObject:");
Method originalMethod = class_getInstanceMethod(cls, originalSelector);
IMP impOvverride = (IMP) interceptIMP;
originalIMP = (void *)method_getImplementation(originalMethod);
method_setImplementation(originalMethod, impOvverride);
}
在iOS最新的11.3更新之后,调用此方法会出现崩溃。经过调试发现,主要是因为调用的该 _startAssistingNode:userIsInteracting:blurPreviousNode:userObject:
私有方法名已经变更为:
_startAssistingNode:userIsInteracting:blurPreviousNode:changingActivityState:userObject:
所以上述方法更新一把(swift):
以此记录,希望可以帮助到你