如何处理iOS Web视图本机键盘上的返回键? [英] How do I handle return key on an iOS web view native keyboard?
问题描述
在我的iOS应用中,我有一个带文本字段的UIWebView。我想回复被点击的Return键。我该怎么做?
In my iOS app, I have a UIWebView with text fields . I'd like to respond to the Return key being tapped. How do I do that?
我可以以某种方式访问UITextField以便我可以实现UITextFieldDelegate方法textFieldShouldReturn吗?或者还有其他方法可以实现这一目标吗?
Can I somehow get access to the UITextField so I can implement UITextFieldDelegate method textFieldShouldReturn? Or is there some other way to accomplish this?
推荐答案
你需要为此做一些JavaScript技巧。
You need to do some JavaScript tricks for this.
在你的HTML文档中,绑定一个用于文本输入控件的keyup事件回调,(上次我检查时,UIWebView不会为返回键触发keypress事件,所以keyup事件是你最好的替代)
In your HTML document, bind a keyup event callback for your text input control, (Last time I checked, somehow UIWebView doesn't fire keypress event for return key, so keyup event is your best alternative)
在此回调中,让Web视图转到自定义URL方案。例如:
In this callback, let the web view goto a custom URL scheme. e.g below:
var input = document.getElementById('textInput');
input.onkeyup = function(){window.location = 'callback://keyup'};
然后你可以在UIWebView的委托中处理这个URL方案
Then you can handle this URL scheme in you UIWebView's delegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([request.URL.scheme isEqualToString:@"callback"]) {
// Do what you want
return NO;
}
return YES;
}
这篇关于如何处理iOS Web视图本机键盘上的返回键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!