Show keyboard when Textfield inside Alert in WkWebView is tapped

390 Views Asked by At

There is a game that i load in the WKWebView and this Alert and textField appear after the game is finished. I want to display keyboard when user taps on the textField. But the problem is there is no code to display this alert and textfield, otherwise i would have managed the textField to becomeFirstResponder().

Desperately need some help. I would really appreciate it.

enter image description here

1

There are 1 best solutions below

0
On BEST ANSWER

Okay so i have found the solution. I had a textField in custom Alert/Popup in WkwebView and i wanted the textField to becomeFirstResponder() so i could take user input.

// So i Implemented WKUIDelegate..

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

Assigned the Delegate..

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

//implemented delegate function.

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

After successful build, this was the output.

  1. I tapped the textField, and javaScript Alert appeared.

enter image description here

  1. This right here is what i wanted to achieve.

enter image description here

Maybe i was not good enough to explain my question, but this is what i was asking for. Thank you :)