Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// declare WKUIDelegate as a delegate for ApplicationController
lazy var session: TLSession = {
let session = TLSession(webViewConfiguration: self.webViewConfiguration)
session.delegate = self
session.webView.UIDelegate = self // add this
return session
}()
// MARK: WKUIDelegate
func webView(webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: () -> Void) {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(defaultAction)
presentViewController(alertController, animated: true, completion: completionHandler)
}
func webView(webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: (Bool) -> Void) {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: {(UIAlertAction) in completionHandler(false) })
let confirmAction = UIAlertAction(title: "OK", style: .Default, handler: {(UIAlertAction) in completionHandler(true) })
alertController.addAction(cancelAction)
alertController.addAction(confirmAction)
presentViewController(alertController, animated: true, completion: nil)
}
func webView(webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: (String?) -> Void) {
let alertController = UIAlertController(title: nil, message: prompt, preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: {(UIAlertAction) in completionHandler(nil)})
let confirmAction = UIAlertAction(title: "OK", style: .Default, handler: {(UIAlertAction) in
let textField = alertController.textFields![0] as UITextField
completionHandler(textField.text)
})
alertController.addTextFieldWithConfigurationHandler({(textField) in textField.text = defaultText})
alertController.addAction(cancelAction)
alertController.addAction(confirmAction)
presentViewController(alertController, animated: true, completion: nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment