소스 검색

Merge pull request #1734 from deltachat/mailto_in_webview

handle mailto addresses in WebViewControllers
cyBerta 2 년 전
부모
커밋
33bbe63311

+ 0 - 10
deltachat-ios/Controller/ConnectivityViewController.swift

@@ -3,21 +3,11 @@ import DcCore
 import Network
 
 class ConnectivityViewController: WebViewViewController {
-    private let dcContext: DcContext
     private var connectivityChangedObserver: NSObjectProtocol?
     private var lowPowerModeObserver: NSObjectProtocol?
     private var connectivityMonitor: AnyObject?
     private var isLowDataMode: Bool = false
 
-    init(dcContext: DcContext) {
-        self.dcContext = dcContext
-        super.init()
-    }
-
-    required init?(coder: NSCoder) {
-        fatalError("init(coder:) has not been implemented")
-    }
-
     // called only once after loading
     override func viewDidLoad() {
         super.viewDidLoad()

+ 1 - 3
deltachat-ios/Controller/FullMessageViewController.swift

@@ -12,7 +12,6 @@ class FullMessageViewController: WebViewViewController {
     }
 
     var messageId: Int
-    var dcContext: DcContext
     private var loadContentOnce = false
 
     // Block just everything :)
@@ -31,9 +30,8 @@ class FullMessageViewController: WebViewViewController {
     
 
     init(dcContext: DcContext, messageId: Int) {
-        self.dcContext = dcContext
         self.messageId = messageId
-        super.init()
+        super.init(dcContext: dcContext)
         self.allowSearch = true
     }
 

+ 2 - 2
deltachat-ios/Controller/HelpViewController.swift

@@ -4,8 +4,8 @@ import DcCore
 
 class HelpViewController: WebViewViewController {
 
-    override init() {
-        super.init()
+    override init(dcContext: DcContext) {
+        super.init(dcContext: dcContext)
         self.allowSearch = true
     }
 

+ 1 - 1
deltachat-ios/Controller/SettingsController.swift

@@ -581,7 +581,7 @@ internal final class SettingsViewController: UITableViewController, ProgressAler
     }
 
     private func showHelp() {
-        navigationController?.pushViewController(HelpViewController(), animated: true)
+        navigationController?.pushViewController(HelpViewController(dcContext: dcContext), animated: true)
     }
 
     private func showConnectivity() {

+ 49 - 1
deltachat-ios/Controller/WebViewViewController.swift

@@ -1,5 +1,6 @@
 import UIKit
 import WebKit
+import DcCore
 
 class WebViewViewController: UIViewController, WKNavigationDelegate {
 
@@ -46,6 +47,7 @@ class WebViewViewController: UIViewController, WKNavigationDelegate {
     private var debounceTimer: Timer?
     private var initializedSearch = false
     open var allowSearch = false
+    var dcContext: DcContext
 
     open var configuration: WKWebViewConfiguration {
         let preferences = WKPreferences()
@@ -59,7 +61,8 @@ class WebViewViewController: UIViewController, WKNavigationDelegate {
         return config
     }
 
-    init() {
+    init(dcContext: DcContext) {
+        self.dcContext = dcContext
         super.init(nibName: nil, bundle: nil)
         hidesBottomBarWhenPushed = true
     }
@@ -69,6 +72,13 @@ class WebViewViewController: UIViewController, WKNavigationDelegate {
     }
 
     func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
+        if let url = navigationAction.request.url {
+            if url.scheme == "mailto" {
+                openChatFor(url: url)
+                decisionHandler(.cancel)
+                return
+            }
+        }
         if navigationAction.navigationType == .linkActivated,
            let url = navigationAction.request.url,
             url.host != nil,
@@ -192,6 +202,44 @@ class WebViewViewController: UIViewController, WKNavigationDelegate {
         webView.evaluateJavaScript("WKWebView_SearchPrev()", completionHandler: nil)
         updateAccessoryBar()
     }
+
+    func openChatFor(url: URL) {
+        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate,
+              let emailAddress = parseEmailAddress(from: url) else {
+            return
+        }
+
+        // FIXME: lookupContactIdByAddress is still broken
+        // let contactId = dcContext.lookupContactIdByAddress(emailAddress)
+
+        // workaround:
+        let contacts: [Int] = dcContext.getContacts(flags: DC_GCL_ADD_SELF, queryString: emailAddress)
+        let index = contacts.firstIndex(where: { dcContext.getContact(id: $0).email == emailAddress }) ?? -1
+        let contactId = index >= 0 ? contacts[index] : 0
+
+        if contactId == 0 {
+            let alert = UIAlertController(title: String.localizedStringWithFormat(String.localized("ask_start_chat_with"), emailAddress),
+                                          message: nil,
+                                          preferredStyle: .safeActionSheet)
+            alert.addAction(UIAlertAction(title: String.localized("start_chat"), style: .default, handler: { _ in
+                RelayHelper.shared.askToChatWithMailto = false
+                _ = appDelegate.application(UIApplication.shared, open: url)
+            }))
+            alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
+            present(alert, animated: true, completion: nil)
+        } else {
+            RelayHelper.shared.askToChatWithMailto = false
+            _ = appDelegate.application(UIApplication.shared, open: url)
+        }
+    }
+
+    private func parseEmailAddress(from url: URL) -> String? {
+        if let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false),
+           !urlComponents.path.isEmpty {
+             return RelayHelper.shared.splitString(urlComponents.path)[0]
+        }
+        return nil
+    }
 }
 
 extension WebViewViewController: UISearchBarDelegate, UISearchControllerDelegate {

+ 2 - 29
deltachat-ios/Controller/WebxdcViewController.swift

@@ -12,7 +12,6 @@ class WebxdcViewController: WebViewViewController {
     let INTERNALSCHEMA = "webxdc"
     
     var messageId: Int
-    var dcContext: DcContext
     var webxdcUpdateObserver: NSObjectProtocol?
     var webxdcName: String = ""
     var sourceCodeUrl: String?
@@ -154,10 +153,9 @@ class WebxdcViewController: WebViewViewController {
     
     
     init(dcContext: DcContext, messageId: Int) {
-        self.dcContext = dcContext
         self.messageId = messageId
         self.shortcutManager = ShortcutManager(dcContext: dcContext, messageId: messageId)
-        super.init()
+        super.init(dcContext: dcContext)
     }
     
     required init?(coder: NSCoder) {
@@ -221,7 +219,7 @@ class WebxdcViewController: WebViewViewController {
         // TODO: what about tel://
         if let url = navigationAction.request.url {
             if url.scheme == "mailto" {
-                askToChatWith(url: url)
+                openChatFor(url: url)
                 decisionHandler(.cancel)
                 return
             } else if url.scheme != INTERNALSCHEMA {
@@ -302,31 +300,6 @@ class WebxdcViewController: WebViewViewController {
             UIApplication.shared.open(url)
         }
     }
-
-    private func askToChatWith(url: URL) {
-        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate,
-              let emailAddress = parseEmailAddress(from: url) else {
-            return
-        }
-
-        let alert = UIAlertController(title: String.localizedStringWithFormat(String.localized("ask_start_chat_with"), emailAddress),
-                                      message: nil,
-                                      preferredStyle: .safeActionSheet)
-        alert.addAction(UIAlertAction(title: String.localized("start_chat"), style: .default, handler: { _ in
-            RelayHelper.shared.askToChatWithMailto = false
-            _ = appDelegate.application(UIApplication.shared, open: url)
-        }))
-        alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
-        present(alert, animated: true, completion: nil)
-    }
-
-    private func parseEmailAddress(from url: URL) -> String? {
-        if let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false),
-           !urlComponents.path.isEmpty {
-             return RelayHelper.shared.splitString(urlComponents.path)[0]
-        }
-        return nil
-    }
 }
 
 extension WebxdcViewController: WKScriptMessageHandler {