From cab486c8bd1201fbc1e7037a76b2da953808c9e4 Mon Sep 17 00:00:00 2001 From: Matthew Noorenberghe Date: Fri, 25 Jan 2019 21:44:55 -0800 Subject: [PATCH] Implement WKUIDelegate runJavaScriptConfirmPanelWithMessage support. Fixes #174 and fixes #156 --- .../Resources/en.lproj/Localizable.strings | 2 ++ HomeAssistant/Views/WebViewController.swift | 16 ++++++++++++++++ Shared/Resources/Swiftgen/Strings.swift | 7 +++++++ 3 files changed, 25 insertions(+) diff --git a/HomeAssistant/Resources/en.lproj/Localizable.strings b/HomeAssistant/Resources/en.lproj/Localizable.strings index 65059d34d..2637a417b 100644 --- a/HomeAssistant/Resources/en.lproj/Localizable.strings +++ b/HomeAssistant/Resources/en.lproj/Localizable.strings @@ -152,6 +152,8 @@ "about.beta.title" = "Join Beta"; "alerts.auth_required.message" = "The server has rejected your credentials, and you must sign in again to continue."; "alerts.auth_required.title" = "You must sign in to continue"; +"alerts.confirm.ok" = "OK"; +"alerts.confirm.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."; diff --git a/HomeAssistant/Views/WebViewController.swift b/HomeAssistant/Views/WebViewController.swift index dc71ee409..a14b40ca4 100644 --- a/HomeAssistant/Views/WebViewController.swift +++ b/HomeAssistant/Views/WebViewController.swift @@ -257,6 +257,22 @@ class WebViewController: UIViewController, WKNavigationDelegate, WKUIDelegate, C self.setToolbarItems(items, animated: true) } + // WKUIDelegate + func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, + initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) { + let alertController = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet) + + alertController.addAction(UIAlertAction(title: L10n.Alerts.Confirm.ok, style: .default, handler: { (action) in + completionHandler(true) + })) + + alertController.addAction(UIAlertAction(title: L10n.Alerts.Confirm.cancel, style: .cancel, handler: { (action) in + completionHandler(false) + })) + + self.present(alertController, animated: true, completion: nil) + } + @objc func loadActiveURLIfNeeded() { if HomeAssistantAPI.authenticatedAPI() != nil, let connectionInfo = Current.settingsStore.connectionInfo, diff --git a/Shared/Resources/Swiftgen/Strings.swift b/Shared/Resources/Swiftgen/Strings.swift index 7ab637836..af9c7272c 100644 --- a/Shared/Resources/Swiftgen/Strings.swift +++ b/Shared/Resources/Swiftgen/Strings.swift @@ -100,6 +100,13 @@ internal enum L10n { internal static let title = L10n.tr("Localizable", "alerts.auth_required.title") } + internal enum Confirm { + /// Cancel + internal static let cancel = L10n.tr("Localizable", "alerts.confirm.cancel") + /// OK + internal static let ok = L10n.tr("Localizable", "alerts.confirm.ok") + } + internal enum OpenUrlFromNotification { /// Open URL (%@) found in notification? internal static func message(_ p1: String) -> String {