How do I handle return key on an iOS web view nati

2019-03-06 23:17发布

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?

Can I somehow get access to the UITextField so I can implement UITextFieldDelegate method textFieldShouldReturn? Or is there some other way to accomplish this?

2条回答
Animai°情兽
2楼-- · 2019-03-06 23:22

uiwebview can not can uitextfieldDelegate . try to read Apple document

查看更多
孤傲高冷的网名
3楼-- · 2019-03-06 23:38

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;
}
查看更多
登录 后发表回答