Skip to content

Commit

Permalink
Implement WKUIDelegate runJavaScriptTextInputPanelWithPrompt support
Browse files Browse the repository at this point in the history
  • Loading branch information
mnoorenberghe committed Jan 26, 2019
1 parent cab486c commit a3aaab6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions HomeAssistant/Resources/en.lproj/Localizable.strings
Expand Up @@ -154,6 +154,8 @@
"alerts.auth_required.title" = "You must sign in to continue";
"alerts.confirm.ok" = "OK";
"alerts.confirm.cancel" = "Cancel";
"alerts.prompt.ok" = "OK";
"alerts.prompt.cancel" = "Cancel";
"cl_error.description.deferred_accuracy_too_low" = "Deferred mode is not supported for the requested accuracy.";
"cl_error.description.deferred_canceled" = "The request for deferred updates was canceled by your app or by the location manager.";
"cl_error.description.deferred_distance_filtered" = "Deferred mode does not support distance filters.";
Expand Down
23 changes: 23 additions & 0 deletions HomeAssistant/Views/WebViewController.swift
Expand Up @@ -273,6 +273,29 @@ class WebViewController: UIViewController, WKNavigationDelegate, WKUIDelegate, C
self.present(alertController, animated: true, completion: nil)
}

func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?,
initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) {
let alertController = UIAlertController(title: nil, message: prompt, preferredStyle: .alert)

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

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

alertController.addAction(UIAlertAction(title: L10n.Alerts.Prompt.cancel, style: .cancel, handler: { (action) in
completionHandler(nil)
}))

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

@objc func loadActiveURLIfNeeded() {
if HomeAssistantAPI.authenticatedAPI() != nil,
let connectionInfo = Current.settingsStore.connectionInfo,
Expand Down
7 changes: 7 additions & 0 deletions Shared/Resources/Swiftgen/Strings.swift
Expand Up @@ -115,6 +115,13 @@ internal enum L10n {
/// Open URL?
internal static let title = L10n.tr("Localizable", "alerts.open_url_from_notification.title")
}

internal enum Prompt {
/// Cancel
internal static let cancel = L10n.tr("Localizable", "alerts.prompt.cancel")
/// OK
internal static let ok = L10n.tr("Localizable", "alerts.prompt.ok")
}
}

internal enum ClError {
Expand Down

0 comments on commit a3aaab6

Please sign in to comment.