断言失败 - [UIAlertController addTextFieldWithConfigurationHandler:]
问题描述:
一个WKWebView内的JavaScript调用window.prompt
产生断言错误:断言失败 - [UIAlertController addTextFieldWithConfigurationHandler:]
Assertion failure in -[UIAlertController addTextFieldWithConfigurationHandler:]
断言误差来源于此WKUIDelegate功能:
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)
}
类文档在添加文本字段或初始化程序时,不要显示添加配置处理程序的方法。那么你应该怎么处理呢?
答
尝试从样式actionSheet
更改为alert
。对于将从UIAlertControllerStyleActionSheet
更改为UIAlertControllerStyleAlert
的Objective-C用户。
答
应该从viewController调用Present。怀疑可能是你的情况。
在调用'present'之前,配置警报控制器时断言似乎正在发生。你能举出一个试图呈现警报的断言失败的例子吗? – matthias