NCBrowserWeb.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. import WebKit
  25. @objc protocol NCBrowserWebDelegate: AnyObject {
  26. @objc optional func browserWebDismiss()
  27. }
  28. class NCBrowserWeb: UIViewController {
  29. var webView: WKWebView?
  30. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  31. @objc var urlBase = ""
  32. @objc var isHiddenButtonExit = false
  33. @objc var titleBrowser: String? = nil
  34. @objc weak var delegate: NCBrowserWebDelegate?
  35. @IBOutlet weak var buttonExit: UIButton!
  36. // MARK: - View Life Cycle
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. webView = WKWebView(frame: CGRect.zero)
  40. webView!.navigationDelegate = self
  41. view.addSubview(webView!)
  42. webView!.translatesAutoresizingMaskIntoConstraints = false
  43. webView!.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
  44. webView!.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
  45. webView!.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
  46. webView!.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
  47. // button exit
  48. if isHiddenButtonExit {
  49. buttonExit.isHidden = true
  50. } else {
  51. self.view.bringSubviewToFront(buttonExit)
  52. let image = NCUtility.shared.loadImage(named: "xmark", color: .systemBlue)
  53. buttonExit.setImage(image, for: .normal)
  54. }
  55. if let url = URL(string: urlBase) {
  56. loadWebPage(webView: webView!, url: url)
  57. } else {
  58. let url = URL(fileURLWithPath: urlBase)
  59. loadWebPage(webView: webView!, url: url)
  60. }
  61. //navigationItem.rightBarButtonItem = UIBarButtonItem.init(image: UIImage(named: "more")!.image(color: NCBrandColor.shared.label, size: 25), style: .plain, target: self, action: #selector(self.openMenuMore))
  62. }
  63. override func viewWillAppear(_ animated: Bool) {
  64. super.viewWillAppear(animated)
  65. if let titleBrowser = titleBrowser {
  66. navigationItem.title = titleBrowser
  67. }
  68. }
  69. deinit {
  70. }
  71. // MARK: - Action
  72. @IBAction func touchUpInsideButtonExit(_ sender: UIButton) {
  73. self.dismiss(animated: true) {
  74. self.delegate?.browserWebDismiss?()
  75. }
  76. }
  77. //
  78. func loadWebPage(webView: WKWebView, url: URL) {
  79. let language = NSLocale.preferredLanguages[0] as String
  80. var request = URLRequest(url: url)
  81. request.addValue("true", forHTTPHeaderField: "OCS-APIRequest")
  82. request.addValue(language, forHTTPHeaderField: "Accept-Language")
  83. webView.customUserAgent = CCUtility.getUserAgent()
  84. webView.load(request)
  85. }
  86. }
  87. extension NCBrowserWeb: WKNavigationDelegate {
  88. public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
  89. if let serverTrust = challenge.protectionSpace.serverTrust {
  90. completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
  91. } else {
  92. completionHandler(URLSession.AuthChallengeDisposition.useCredential, nil);
  93. }
  94. }
  95. public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
  96. decisionHandler(.allow)
  97. }
  98. public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  99. print("didStartProvisionalNavigation");
  100. }
  101. public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  102. print("didFinishProvisionalNavigation");
  103. }
  104. public func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
  105. print("didReceiveServerRedirectForProvisionalNavigation");
  106. }
  107. }