NCViewerRichWorkspaceWebView.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // NCViewerRichWorkspaceWebView.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 12/12/19.
  6. // Copyright © 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. class NCViewerRichWorkspaceWebView: UIViewController, WKNavigationDelegate, WKScriptMessageHandler {
  26. @IBOutlet weak var webView: WKWebView!
  27. @IBOutlet weak var webViewBottomConstraint: NSLayoutConstraint!
  28. var metadata: tableMetadata?
  29. var url: String = ""
  30. // MARK: - View Life Cycle
  31. override func viewDidLoad() {
  32. super.viewDidLoad()
  33. NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)
  34. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
  35. var request = URLRequest(url: URL(string: url)!)
  36. request.addValue("true", forHTTPHeaderField: "OCS-APIRequest")
  37. let language = NSLocale.preferredLanguages[0] as String
  38. request.addValue(language, forHTTPHeaderField: "Accept-Language")
  39. webView.configuration.userContentController.add(self, name: "DirectEditingMobileInterface")
  40. webView.navigationDelegate = self
  41. webView.customUserAgent = userAgent
  42. webView.load(request)
  43. }
  44. deinit {
  45. print("dealloc")
  46. }
  47. override func viewWillDisappear(_ animated: Bool) {
  48. super.viewWillDisappear(animated)
  49. webView.configuration.userContentController.removeScriptMessageHandler(forName: "DirectEditingMobileInterface")
  50. }
  51. @objc func keyboardDidShow(notification: Notification) {
  52. let window = UIApplication.shared.connectedScenes.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }.first { $0.isKeyWindow }
  53. let safeAreaInsetsBottom = window?.safeAreaInsets.bottom ?? 0
  54. guard let info = notification.userInfo else { return }
  55. guard let frameInfo = info[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
  56. let keyboardFrame = frameInfo.cgRectValue
  57. webViewBottomConstraint.constant = keyboardFrame.size.height - safeAreaInsetsBottom
  58. }
  59. @objc func keyboardWillHide(notification: Notification) {
  60. webViewBottomConstraint.constant = 0
  61. }
  62. // MARK: -
  63. public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
  64. if message.name == "DirectEditingMobileInterface" {
  65. if message.body as? String == "close" {
  66. self.presentationController?.delegate?.presentationControllerWillDismiss?(self.presentationController!)
  67. dismiss(animated: true) {
  68. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterCloseRichWorkspaceWebView, userInfo: nil)
  69. }
  70. }
  71. if message.body as? String == "share" {
  72. if metadata != nil {
  73. NCActionCenter.shared.openShare(viewController: self, metadata: metadata!, page: .sharing)
  74. }
  75. }
  76. if message.body as? String == "loading" {
  77. print("loading")
  78. }
  79. if message.body as? String == "loaded" {
  80. print("loaded")
  81. }
  82. if message.body as? String == "paste" {
  83. self.paste(self)
  84. }
  85. }
  86. }
  87. // MARK: -
  88. public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
  89. DispatchQueue.global().async {
  90. if let serverTrust = challenge.protectionSpace.serverTrust {
  91. completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
  92. } else {
  93. completionHandler(URLSession.AuthChallengeDisposition.useCredential, nil)
  94. }
  95. }
  96. }
  97. public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  98. print("didStartProvisionalNavigation")
  99. }
  100. public func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
  101. print("didReceiveServerRedirectForProvisionalNavigation")
  102. }
  103. public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  104. print("didFinish")
  105. }
  106. }