NCBrowserWeb.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // NCBrowserWeb.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 22/08/2019.
  6. // Copyright (c) 2019 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. @preconcurrency import WebKit
  25. @objc protocol NCBrowserWebDelegate: AnyObject {
  26. @objc optional func browserWebDismiss()
  27. }
  28. class NCBrowserWeb: UIViewController {
  29. var urlBase = ""
  30. var isHiddenButtonExit = false
  31. var titleBrowser: String?
  32. weak var delegate: NCBrowserWebDelegate?
  33. @IBOutlet weak var buttonExit: UIButton!
  34. // MARK: - View Life Cycle
  35. override func viewDidLoad() {
  36. super.viewDidLoad()
  37. let webView = WKWebView(frame: CGRect.zero)
  38. webView.navigationDelegate = self
  39. view.addSubview(webView)
  40. webView.translatesAutoresizingMaskIntoConstraints = false
  41. webView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
  42. webView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
  43. webView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
  44. webView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
  45. if isHiddenButtonExit {
  46. buttonExit.isHidden = true
  47. } else {
  48. self.view.bringSubviewToFront(buttonExit)
  49. let image = NCUtility().loadImage(named: "xmark", colors: [.systemBlue])
  50. buttonExit.setImage(image, for: .normal)
  51. }
  52. if let url = URL(string: urlBase) {
  53. loadWebPage(webView: webView, url: url)
  54. } else {
  55. let url = URL(fileURLWithPath: urlBase)
  56. loadWebPage(webView: webView, url: url)
  57. }
  58. }
  59. override func viewWillAppear(_ animated: Bool) {
  60. super.viewWillAppear(animated)
  61. if let titleBrowser = titleBrowser {
  62. navigationItem.title = titleBrowser
  63. }
  64. }
  65. deinit {
  66. }
  67. // MARK: - Action
  68. @IBAction func touchUpInsideButtonExit(_ sender: UIButton) {
  69. self.dismiss(animated: true) {
  70. self.delegate?.browserWebDismiss?()
  71. }
  72. }
  73. func loadWebPage(webView: WKWebView, url: URL) {
  74. let language = NSLocale.preferredLanguages[0] as String
  75. var request = URLRequest(url: url)
  76. request.addValue("true", forHTTPHeaderField: "OCS-APIRequest")
  77. request.addValue(language, forHTTPHeaderField: "Accept-Language")
  78. webView.customUserAgent = userAgent
  79. webView.load(request)
  80. }
  81. }
  82. extension NCBrowserWeb: WKNavigationDelegate {
  83. public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
  84. DispatchQueue.global().async {
  85. if let serverTrust = challenge.protectionSpace.serverTrust {
  86. completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
  87. } else {
  88. completionHandler(URLSession.AuthChallengeDisposition.useCredential, nil)
  89. }
  90. }
  91. }
  92. public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
  93. DispatchQueue.global().async {
  94. decisionHandler(.allow)
  95. }
  96. }
  97. }