场景
针对ios和android端需要在H5中首次直接拉起原生键盘,此处需添加原生代码处理
android
原生中模拟点击事件触发,代码如下
public void showSoftInputMethod(final Callback callback) {
runOnMainThread(new Runnable() {
@Override
public void run() {
WebView webView = mWebViewRef.get();
if (webView != null) {
webView.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0));
webView.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 0, 0, 0));
callback.invoke(SUCCESS, "SUCCESS");
} else {
callback.invoke(ERROR, "current webView is null");
}
}});
}
IOS
UIWebView
想一开始唤起键盘,除了web端需要设置input 的focus状态外,我们还需要将keyboardDisplayRequiresUserAction设置为false
WKWebView
WKWebView是没有keyboardDisplayRequiresUserAction这个属性的,但又想做想一开始就能唤起键盘,怎么办呢?只能通过runtime处理了,以下是收集Stack Overflow的方法
#import<objc/runtime.h>
@implementationWebViewInjection
+ (void)allowDisplayingKeyboardWithoutUserAction:(BOOL)allow {
Class class = NSClassFromString(@"WKContentView");
NSOperatingSystemVersion iOS_11_3_0 = (NSOperatingSystemVersion){11, 3, 0};
NSOperatingSystemVersion iOS_12_2_0 = (NSOperatingSystemVersion){12, 2, 0};
if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: iOS_12_2_0]) {
SEL selector = sel_getUid("_elementDidFocus:userIsInteracting:blurPreviousNode:changingActivityState:userObject:");
Methodmethod =class_getInstanceMethod(class, selector);
if(allowMethod==0x0) {
IMPoriginal =method_getImplementation(method);
allowMethod=imp_implementationWithBlock(^void(idme,void* arg0,BOOLarg1,BOOLarg2,BOOLarg3,idarg4) {
((void(*)(id,SEL,void*,BOOL,BOOL,BOOL,id))original)(me, selector, arg0,TRUE, arg2, arg3, arg4);
});
disAllowMethod= original;
}
if(allow) {
method_setImplementation(method, allowMethod);
}else{
method_setImplementation(method, disAllowMethod);
}
}
else if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: iOS_11_3_0]) {
SEL selector = sel_getUid("_startAssistingNode:userIsInteracting:blurPreviousNode:changingActivityState:userObject:");
Methodmethod =class_getInstanceMethod(class, selector);
if(allowMethod==0x0) {
IMPoriginal =method_getImplementation(method);
allowMethod=imp_implementationWithBlock(^void(idme,void* arg0,BOOLarg1,BOOLarg2,BOOLarg3,idarg4) {
((void(*)(id,SEL,void*,BOOL,BOOL,BOOL,id))original)(me, selector, arg0,TRUE, arg2, arg3, arg4);
});
disAllowMethod= original;
}
if(allow) {
method_setImplementation(method, allowMethod);
}
else{
method_setImplementation(method, disAllowMethod);
}
}else{
SEL selector = sel_getUid("_startAssistingNode:userIsInteracting:blurPreviousNode:userObject:");
Methodmethod =class_getInstanceMethod(class, selector);
if(allowMethod==0x0) {
IMPoriginal =method_getImplementation(method);
allowMethod=imp_implementationWithBlock(^void(idme,void* arg0,BOOLarg1,BOOLarg2,idarg3) {
((void(*)(id,SEL,void*,BOOL,BOOL,id))original)(me, selector, arg0,TRUE, arg2, arg3);
});
disAllowMethod= original;
}
if(allow) {
method_setImplementation(method, allowMethod);
}else{
method_setImplementation(method, disAllowMethod);
}
}
}
IOS 端可参看原文地址:https://stackoverflow.com/questions/32449870/programmatically-focus-on-a-form-in-a-webview-wkwebview
此方法需要开始自动弹起键盘就传递YES的值就可以了!