How do I handle return key on an iOS web view native keyboard?

怎甘沉沦 提交于 2019-12-02 12:55:33

You need to do some JavaScript tricks for this.

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)

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'};

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;
}

uiwebview can not can uitextfieldDelegate . try to read Apple document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!