Crashalot
Crashalot

Reputation: 34503

Assertion failure in -[UIAlertController addTextFieldWithConfigurationHandler:]

Invoking window.prompt in JavaScript within a WKWebView generates an assertion error:

Assertion failure in -[UIAlertController addTextFieldWithConfigurationHandler:]

The assertion error comes from this WKUIDelegate function:

func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo,
             completionHandler: @escaping (String?) -> Void) {

    let alertController = UIAlertController(title: nil, message: prompt, preferredStyle: .actionSheet)

    alertController.addTextField { (textField) in
        textField.text = defaultText
    }

    alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
        if let text = alertController.textFields?.first?.text {
            completionHandler(text)
        } else {
            completionHandler(defaultText)
        }
    }))

    alertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
        completionHandler(nil)
    }))

    present(alertController, animated: true, completion: nil)
}

The class docs don't show a way to add a configuration handler when adding a text field nor in the initializer. So how are you supposed to handle this?

Upvotes: 1

Views: 822

Answers (2)

AshwiniM
AshwiniM

Reputation: 73

Present should be called from viewController. Is suspect that might be the case with you.

Upvotes: 0

Andreas K
Andreas K

Reputation: 590

Try changing from style actionSheet to alert. For Objective-C user that would be changing from UIAlertControllerStyleActionSheet to UIAlertControllerStyleAlert.

Upvotes: 1

Related Questions