場景?
針對ios和android端需要在H5中首次直接拉起原生鍵盤敏沉,此處需添加原生代碼處理
android
原生中模擬點(diǎn)擊事件觸發(fā)初厚,代碼如下
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端需要設(shè)置input 的focus狀態(tài)外郑叠,我們還需要將keyboardDisplayRequiresUserAction設(shè)置為false
WKWebView
WKWebView是沒有keyboardDisplayRequiresUserAction這個(gè)屬性的夜赵,但又想做想一開始就能喚起鍵盤,怎么辦呢锻拘?只能通過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
此方法需要開始自動(dòng)彈起鍵盤就傳遞YES的值就可以了!