PrivacyPolicyViewController.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2017-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import UIKit
  21. import ThreemaFramework
  22. import WebKit
  23. class PrivacyPolicyViewController: UIViewController, WKNavigationDelegate {
  24. var webView: WKWebView!
  25. override func viewDidLoad() {
  26. super.viewDidLoad()
  27. self.view.backgroundColor = Colors.background()
  28. let webPreferences = WKPreferences.init()
  29. webPreferences.javaScriptEnabled = false
  30. let webConfiguration = WKWebViewConfiguration()
  31. webConfiguration.preferences = webPreferences
  32. webView = WKWebView.init(frame: .zero, configuration: webConfiguration)
  33. webView.allowsLinkPreview = false
  34. webView.navigationDelegate = self
  35. webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  36. webView!.isOpaque = false
  37. view = webView
  38. }
  39. override func viewWillAppear(_ animated: Bool) {
  40. super.viewWillAppear(animated)
  41. webView.backgroundColor = Colors.background()
  42. var lang:String? = Bundle.main.preferredLocalizations.first
  43. if lang == nil {
  44. lang = "en";
  45. }
  46. var version = BundleUtil.mainBundle().object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
  47. let suffix = BundleUtil.mainBundle().object(forInfoDictionaryKey: "ThreemaVersionSuffix") as? String
  48. if suffix != nil {
  49. version = version.appending(suffix!)
  50. }
  51. var theme:String! = ""
  52. switch Colors.getTheme() {
  53. case ColorThemeDark, ColorThemeDarkWork:
  54. theme = "dark"
  55. break
  56. case ColorThemeUndefined, ColorThemeLight, ColorThemeLightWork:
  57. theme = "light"
  58. break
  59. default:
  60. theme = "light"
  61. break
  62. }
  63. if self.navigationController?.viewControllers.count == 1 {
  64. let doneButton: UIBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: .done, target:self, action:#selector(donePressed))
  65. self.navigationItem.rightBarButtonItem = doneButton
  66. theme = "dark"
  67. webView.backgroundColor = Colors.backgroundThemeDark()
  68. self.navigationController?.navigationBar.barStyle = .black
  69. self.navigationController?.navigationBar.isTranslucent = true
  70. self.navigationController?.navigationBar.tintColor = Colors.mainThemeDark()
  71. }
  72. let urlString:String = String(format:"https://threema.ch/privacy_policy/?lang=%@&version=%@&platform=ios&theme=%@", lang!, version, theme)
  73. let privacyUrl:URL = URL(string: urlString)!
  74. MBProgressHUD.showAdded(to: view, animated: true)
  75. let request = URLRequest.init(url: privacyUrl, cachePolicy: .reloadIgnoringCacheData)
  76. webView.load(request)
  77. }
  78. override func didReceiveMemoryWarning() {
  79. super.didReceiveMemoryWarning()
  80. // Dispose of any resources that can be recreated.
  81. }
  82. func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  83. MBProgressHUD.hide(for: view, animated: true)
  84. }
  85. func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
  86. if navigationAction.navigationType == .linkActivated {
  87. if let url = navigationAction.request.url {
  88. UIApplication.shared.open(url, options: [:], completionHandler: nil)
  89. decisionHandler(.cancel)
  90. return
  91. }
  92. }
  93. decisionHandler(.allow)
  94. }
  95. func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
  96. MBProgressHUD.hide(for: view, animated: true)
  97. UIAlertTemplate.showAlert(owner: self, title: BundleUtil.localizedString(forKey: "cannot_connect_title"), message: BundleUtil.localizedString(forKey: "cannot_connect_message")) { (okAction) in
  98. self.navigationController?.dismiss(animated: true, completion: nil)
  99. }
  100. }
  101. override var shouldAutorotate: Bool {
  102. return true
  103. }
  104. override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
  105. return .allButUpsideDown
  106. }
  107. @objc func donePressed() {
  108. self.navigationController?.dismiss(animated: true, completion: nil)
  109. }
  110. }
  111. extension String {
  112. func hexadecimal() -> Data? {
  113. var data = Data(capacity: self.count / 2)
  114. let regex = try! NSRegularExpression(pattern: "[0-9a-f]{1,2}", options: .caseInsensitive)
  115. regex.enumerateMatches(in: self, range: NSMakeRange(0, utf16.count)) { match, flags, stop in
  116. let byteString = (self as NSString).substring(with: match!.range)
  117. var num = UInt8(byteString, radix: 16)!
  118. data.append(&num, count: 1)
  119. }
  120. guard data.count > 0 else { return nil }
  121. return data
  122. }
  123. }
  124. extension Data {
  125. struct HexEncodingOptions: OptionSet {
  126. let rawValue: Int
  127. static let upperCase = HexEncodingOptions(rawValue: 1 << 0)
  128. }
  129. func hexEncodedString(options: HexEncodingOptions = []) -> String {
  130. let format = options.contains(.upperCase) ? "%02hhX" : "%02hhx"
  131. return map { String(format: format, $0) }.joined()
  132. }
  133. }