2016-11-05 134 views
1

一個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) 
} 

類文檔在添加文本字段或初始化程序時,不要顯示添加配置處理程序的方法。那麼你應該怎麼處理呢?

回答

0

嘗試從樣式actionSheet更改爲alert。對於將從UIAlertControllerStyleActionSheet更改爲UIAlertControllerStyleAlert的Objective-C用戶。

0

應該從viewController調用Present。懷疑可能是你的情況。

+1

在調用'present'之前,配置警報控制器時斷言似乎正在發生。你能舉出一個試圖呈現警報的斷言失敗的例子嗎? – matthias