IOS-1222 adds UrlClickHandlerProtocol
authorbuff <andreas@pep-project.org>
Thu, 02 Aug 2018 16:49:48 +0200
changeset 56719fab3c8132a6
parent 5670 f1417d86bf7c
child 5672 9219c12f10c0
IOS-1222 adds UrlClickHandlerProtocol
pEpForiOS.xcodeproj/project.pbxproj
pEpForiOS/UI/Util/SecureWebViewController/SecureWebViewController.swift
pEpForiOS/UI/Util/UrlClickHandlerProtocol.swift
     1.1 --- a/pEpForiOS.xcodeproj/project.pbxproj	Thu Aug 02 15:15:02 2018 +0200
     1.2 +++ b/pEpForiOS.xcodeproj/project.pbxproj	Thu Aug 02 16:49:48 2018 +0200
     1.3 @@ -96,6 +96,7 @@
     1.4  		1568FEA11FACDCC600993EA3 /* IOS-211_hi_there.txt in Resources */ = {isa = PBXBuildFile; fileRef = 1568FEA01FACDCC500993EA3 /* IOS-211_hi_there.txt */; };
     1.5  		1569AEA520E14DC3002102A0 /* ReUploadTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1569AEA420E14DC2002102A0 /* ReUploadTest.swift */; };
     1.6  		157455C81FDFD2D4008CA78F /* NSAttributedString+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 157455C71FDFD2D4008CA78F /* NSAttributedString+Extensions.swift */; };
     1.7 +		1574D078211356E000FEDC93 /* UrlClickHandlerProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1574D077211356E000FEDC93 /* UrlClickHandlerProtocol.swift */; };
     1.8  		1579397B1F4E00AF00A2A6CF /* UINavigationController+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1579397A1F4E00AF00A2A6CF /* UINavigationController+Extensions.swift */; };
     1.9  		15865A8920319ADC00F7A4B5 /* Folder+pEp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15865A8820319ADC00F7A4B5 /* Folder+pEp.swift */; };
    1.10  		1587D1632049BE7D002C2157 /* ImapConnectionDataCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1587D1622049BE7D002C2157 /* ImapConnectionDataCache.swift */; };
    1.11 @@ -613,6 +614,7 @@
    1.12  		1568FEA01FACDCC500993EA3 /* IOS-211_hi_there.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "IOS-211_hi_there.txt"; sourceTree = "<group>"; };
    1.13  		1569AEA420E14DC2002102A0 /* ReUploadTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReUploadTest.swift; sourceTree = "<group>"; };
    1.14  		157455C71FDFD2D4008CA78F /* NSAttributedString+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSAttributedString+Extensions.swift"; sourceTree = "<group>"; };
    1.15 +		1574D077211356E000FEDC93 /* UrlClickHandlerProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UrlClickHandlerProtocol.swift; sourceTree = "<group>"; };
    1.16  		1579397A1F4E00AF00A2A6CF /* UINavigationController+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UINavigationController+Extensions.swift"; sourceTree = "<group>"; };
    1.17  		15865A8820319ADC00F7A4B5 /* Folder+pEp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Folder+pEp.swift"; sourceTree = "<group>"; };
    1.18  		1587D1622049BE7D002C2157 /* ImapConnectionDataCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImapConnectionDataCache.swift; sourceTree = "<group>"; };
    1.19 @@ -1836,6 +1838,7 @@
    1.20  				4395CDEB20AAB2E9003FC5F1 /* UIImage+GIF.swift */,
    1.21  				490CEBA62100EAD500E8579C /* SelfDismissable.swift */,
    1.22  				B761A80920F5F0A6000832C2 /* UIBarButtonItem+Extension.swift */,
    1.23 +				1574D077211356E000FEDC93 /* UrlClickHandlerProtocol.swift */,
    1.24  			);
    1.25  			path = Util;
    1.26  			sourceTree = "<group>";
    1.27 @@ -2710,6 +2713,7 @@
    1.28  				4362399B1EAE0CE800BD2EB9 /* Date+Extension.swift in Sources */,
    1.29  				43040A531E9776220083DED8 /* AttachmentSummaryView.swift in Sources */,
    1.30  				431D60DB1E93BB2D001266D7 /* AttachmentsView.swift in Sources */,
    1.31 +				1574D078211356E000FEDC93 /* UrlClickHandlerProtocol.swift in Sources */,
    1.32  				150707DA20FFC31E00AA213F /* ComposeUtil.swift in Sources */,
    1.33  				43ED53791CC77F95006AB156 /* UserInfoTableViewController.swift in Sources */,
    1.34  				43C322101EA91764005073FB /* UIImage+Extension.swift in Sources */,
     2.1 --- a/pEpForiOS/UI/Util/SecureWebViewController/SecureWebViewController.swift	Thu Aug 02 15:15:02 2018 +0200
     2.2 +++ b/pEpForiOS/UI/Util/SecureWebViewController/SecureWebViewController.swift	Thu Aug 02 16:49:48 2018 +0200
     2.3 @@ -12,8 +12,9 @@
     2.4      /// Called after the webview has finished loadding and layouting its subviews.
     2.5      /// - Parameters:
     2.6      ///   - webViewController: calling view controller
     2.7 -    ///   - size: webview.scrollview.contentSize after loading html content and layouting
     2.8 -    func secureWebViewController(_ webViewController: SecureWebViewController, sizeChangedTo size: CGSize)
     2.9 +    ///   - sizeChangedTo: webview.scrollview.contentSize after loading html content and layouting
    2.10 +    func secureWebViewController(_ webViewController: SecureWebViewController,
    2.11 +                                 sizeChangedTo size: CGSize)
    2.12  }
    2.13  
    2.14  /// Webview that does not:
    2.15 @@ -47,7 +48,8 @@
    2.16              }
    2.17          }
    2.18      }
    2.19 -    var delegate: SecureWebViewControllerDelegate?
    2.20 +    var delegate: SecureWebViewControllerDelegate? //IOS-1222 TODO: make weak
    2.21 +    weak var urlClickHandler: UrlClickHandlerProtocol?
    2.22  
    2.23      // MARK: - Life Cycle
    2.24  
    2.25 @@ -114,7 +116,7 @@
    2.26                  // We ignore it.
    2.27                  return
    2.28              }
    2.29 -            compiledBlockList = loadedRuleList  
    2.30 +            compiledBlockList = loadedRuleList
    2.31          }
    2.32          loadGroup.notify(queue: DispatchQueue.main) {
    2.33              if compiledBlockList != nil {
    2.34 @@ -155,23 +157,23 @@
    2.35      ///     and loaded locally by CidHandler.
    2.36      private var blockRulesJson: String {
    2.37          return """
    2.38 -         [{
    2.39 -             "trigger": {
    2.40 -                 "url-filter": ".*"
    2.41 -             },
    2.42 -             "action": {
    2.43 -                 "type": "block"
    2.44 -             }
    2.45 +        [{
    2.46 +            "trigger": {
    2.47 +                "url-filter": ".*"
    2.48 +            },
    2.49 +            "action": {
    2.50 +                "type": "block"
    2.51 +            }
    2.52          },
    2.53          {
    2.54              "trigger": {
    2.55                  "url-filter": "cid"
    2.56 -        },
    2.57 +            },
    2.58              "action": {
    2.59                  "type": "ignore-previous-rules"
    2.60 -        }
    2.61 +            }
    2.62          }]
    2.63 -      """
    2.64 +        """
    2.65      }
    2.66  
    2.67      // MARK: - Handle Content Size Changes
    2.68 @@ -248,7 +250,7 @@
    2.69              a:link {
    2.70                  color:\(UIColor.pEpDarkGreenHex);
    2.71                  text-decoration: underline;
    2.72 -            }
    2.73 +        }
    2.74          """
    2.75          let tweak = """
    2.76              \(scaleToFitHtml)
    2.77 @@ -263,17 +265,17 @@
    2.78          if html.contains(find: "<head>") {
    2.79              result = html.replacingOccurrences(of: "<head>", with:
    2.80                  """
    2.81 -        <head>
    2.82 -            \(tweak)
    2.83 -        """)
    2.84 +                <head>
    2.85 +                \(tweak)
    2.86 +                """)
    2.87          } else if html.contains(find: "<html>") {
    2.88              result = html.replacingOccurrences(of: "<html>", with:
    2.89                  """
    2.90 -        <html>
    2.91 -                <head>
    2.92 -                    \(tweak)
    2.93 -                </head>
    2.94 -        """
    2.95 +                <html>
    2.96 +                    <head>
    2.97 +                        \(tweak)
    2.98 +                    </head>
    2.99 +                """
   2.100              )
   2.101          }
   2.102          return result
   2.103 @@ -318,12 +320,22 @@
   2.104              decisionHandler(.allow)
   2.105              return
   2.106          case .linkActivated:
   2.107 -            // Open clicked links in external apps
   2.108 -            guard let newURL = navigationAction.request.url, UIApplication.shared.canOpenURL(newURL)
   2.109 -                else {
   2.110 +            guard let url = navigationAction.request.url else {
   2.111 +                Log.shared.errorAndCrash(component: #function,
   2.112 +                                         errorString: "Link to nonexisting URL has been clicked?")
   2.113 +                break
   2.114 +            }
   2.115 +            if url.scheme == "mailto" {
   2.116 +                // The user clicked on an email URL.
   2.117 +                urlClickHandler?.didClickMailToUrlLink(sender: self, url: url)
   2.118 +            } else {
   2.119 +                // The user clicked a links we do not allow custom handling for.
   2.120 +                // Try to open it in an appropriate app, do nothing if that fails.
   2.121 +                guard UIApplication.shared.canOpenURL(url) else {
   2.122                      break
   2.123 +                }
   2.124 +                UIApplication.shared.openURL(url)
   2.125              }
   2.126 -            UIApplication.shared.openURL(newURL)
   2.127          case .backForward, .formResubmitted, .formSubmitted, .reload:
   2.128              // ignore
   2.129              break
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/pEpForiOS/UI/Util/UrlClickHandlerProtocol.swift	Thu Aug 02 16:49:48 2018 +0200
     3.3 @@ -0,0 +1,17 @@
     3.4 +//
     3.5 +//  UrlClickHandlerProtocol.swift
     3.6 +//  pEp
     3.7 +//
     3.8 +//  Created by Andreas Buff on 02.08.18.
     3.9 +//  Copyright © 2018 p≡p Security S.A. All rights reserved.
    3.10 +//
    3.11 +
    3.12 +import Foundation
    3.13 +
    3.14 +protocol UrlClickHandlerProtocol: class {
    3.15 +    /// Called whenever a mailto:// URL has been clicked by the user.
    3.16 +    /// - Parameters:
    3.17 +    ///   - sender: caller of the message
    3.18 +    ///   - mailToUrlClicked: the clicked URL
    3.19 +    func didClickMailToUrlLink(sender: AnyObject, url: URL)
    3.20 +}