ThreemaWorkViewController.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 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 Foundation
  21. import WebKit
  22. class ThreemaWorkViewController: ThemedViewController {
  23. var webView: WKWebView?
  24. override var shouldAutorotate: Bool {
  25. return true
  26. }
  27. override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
  28. if UIDevice.current.userInterfaceIdiom == .pad {
  29. return .all
  30. }
  31. return .allButUpsideDown
  32. }
  33. override func viewDidLoad() {
  34. super.viewDidLoad()
  35. self.view.backgroundColor = Colors.background()
  36. webView = WKWebView.init(frame: view.frame)
  37. webView!.allowsLinkPreview = false
  38. webView!.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  39. webView!.navigationDelegate = self
  40. webView!.isOpaque = false
  41. view.addSubview(webView!)
  42. }
  43. override func viewWillAppear(_ animated: Bool) {
  44. super.viewWillAppear(animated)
  45. var lang:String? = Bundle.main.preferredLocalizations.first
  46. if lang == nil {
  47. lang = "en";
  48. }
  49. var version = BundleUtil.mainBundle().object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
  50. let suffix = BundleUtil.mainBundle().object(forInfoDictionaryKey: "ThreemaVersionSuffix") as? String
  51. if suffix != nil {
  52. version = version.appending(suffix!)
  53. }
  54. var theme:String! = ""
  55. switch Colors.getTheme() {
  56. case ColorThemeDark, ColorThemeDarkWork:
  57. theme = "dark"
  58. break
  59. case ColorThemeUndefined, ColorThemeLight, ColorThemeLightWork:
  60. theme = "light"
  61. break
  62. default:
  63. theme = "light"
  64. break
  65. }
  66. let urlString:String = String(format:"https://threema.ch/work_info/?lang=%@&version=%@&platform=ios&theme=%@", lang!, version, theme)
  67. let threemaWorkUrl:URL = URL(string: urlString)!
  68. MBProgressHUD.showAdded(to: view, animated: true)
  69. let request = URLRequest.init(url: threemaWorkUrl, cachePolicy: .reloadIgnoringCacheData)
  70. webView!.load(request)
  71. }
  72. }
  73. extension ThreemaWorkViewController: WKNavigationDelegate {
  74. func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  75. MBProgressHUD.hide(for: view, animated: true)
  76. }
  77. func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
  78. if navigationAction.navigationType == .linkActivated {
  79. if navigationAction.request.url != nil {
  80. UIApplication.shared.open(navigationAction.request.url!, options: [:], completionHandler: nil)
  81. decisionHandler(.cancel)
  82. return
  83. }
  84. }
  85. decisionHandler(.allow)
  86. }
  87. }