当点击WkWebView中Alert中的Textfield时显示键盘

问题描述

我在WKWebView中加载了一个游戏,游戏结束后会显示此Alert和textField。我想在用户点击textField时显示键盘。但是问题是没有代码显示此警报和文本字段,否则我本来可以将textField管理为FirstResponder()。

迫切需要一些帮助。我真的很感激。

enter image description here

解决方法

好的,所以我找到了解决方案。我在WkwebView的自定义Alert / Popup中有一个textField,我希望textField为becomeFirstResponder(),这样我才能接受用户输入。

//所以我实现了WKUIDelegate ..

#import <WebKit/WebKit.h>
@interface MyController : UIViewController<WKUIDelegate>

已指派代表。

- (void)viewDidLoad {
    [super viewDidLoad];
     _webkitView.UIDelegate = self;
}

//已实现的委托函数。

- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString *result))completionHandler
{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"BlockChaid Id,Email or Username"
                                                                   message:prompt
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = prompt;
        textField.secureTextEntry = NO;
        textField.text = defaultText;
    }];
    
    [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        completionHandler(nil);
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK",nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        completionHandler([alert.textFields.firstObject text]);
    }]];
    
    [self presentViewController:alert animated:YES completion:nil];
}

成功构建后,这就是输出。

  1. 我点击了textField,然后出现了javaScript Alert。

enter image description here

  1. 这就是我想要实现的目标。

enter image description here

也许我不足以解释我的问题,但这就是我要的。谢谢:)