android键盘自动弹起来了,H5 input 第一次自动弹起IOS和Android中的键盘

场景

针对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

@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);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值