NCViewerNextcloudText.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // NCViewerNextcloudText.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 Foundation
  24. class NCViewerNextcloudText: WKWebView, WKNavigationDelegate, WKScriptMessageHandler {
  25. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  26. var detail: CCDetail!
  27. var editor: String!
  28. var metadata: tableMetadata!
  29. var documentInteractionController: UIDocumentInteractionController!
  30. override init(frame: CGRect, configuration: WKWebViewConfiguration) {
  31. super.init(frame: frame, configuration: configuration)
  32. let contentController = configuration.userContentController
  33. contentController.add(self, name: "DirectEditingMobileInterface")
  34. autoresizingMask = [.flexibleWidth, .flexibleHeight]
  35. navigationDelegate = self
  36. }
  37. required init?(coder: NSCoder) {
  38. super.init(coder: coder)
  39. }
  40. @objc func viewerAt(_ link: String, detail: CCDetail, metadata: tableMetadata, editor: String) {
  41. self.detail = detail
  42. self.metadata = metadata
  43. self.editor = editor;
  44. NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)
  45. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
  46. if (UIDevice.current.userInterfaceIdiom == .phone && editor == k_editor_text) {
  47. detail.navigationController?.setNavigationBarHidden(true, animated: false)
  48. }
  49. var request = URLRequest(url: URL(string: link)!)
  50. request.addValue("true", forHTTPHeaderField: "OCS-APIRequest")
  51. let language = NSLocale.preferredLanguages[0] as String
  52. request.addValue(language, forHTTPHeaderField: "Accept-Language")
  53. let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString")!
  54. if UIDevice.current.userInterfaceIdiom == .pad {
  55. customUserAgent = "Mozilla/5.0 (iOS) Nextcloud-iOS/\(appVersion)"
  56. }else{
  57. customUserAgent = "Mozilla/5.0 (iOS) Mobile Nextcloud-iOS/\(appVersion)"
  58. }
  59. load(request)
  60. }
  61. @objc func keyboardDidShow(notification: Notification) {
  62. guard let info = notification.userInfo else { return }
  63. guard let frameInfo = info[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
  64. let keyboardFrame = frameInfo.cgRectValue
  65. //print("keyboardFrame: \(keyboardFrame)")
  66. frame.size.height = detail.view.bounds.height - keyboardFrame.size.height
  67. }
  68. @objc func keyboardWillHide(notification: Notification) {
  69. frame = detail.view.bounds
  70. }
  71. //MARK: -
  72. public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
  73. if (message.name == "DirectEditingMobileInterface") {
  74. if message.body as? String == "close" {
  75. removeFromSuperview()
  76. detail.navigationController?.popViewController(animated: true)
  77. detail.navigationController?.navigationBar.topItem?.title = ""
  78. appDelegate.activeMain.readFileReloadFolder()
  79. }
  80. if message.body as? String == "share" {
  81. NCMainCommon.sharedInstance.openShare(ViewController: detail, metadata: metadata, indexPage: 2)
  82. }
  83. if message.body as? String == "loading" {
  84. print("loading")
  85. }
  86. if message.body as? String == "loaded" {
  87. print("loaded")
  88. }
  89. if message.body as? String == "paste" {
  90. self.paste(self)
  91. }
  92. }
  93. }
  94. //MARK: -
  95. public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
  96. if let serverTrust = challenge.protectionSpace.serverTrust {
  97. completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
  98. } else {
  99. completionHandler(URLSession.AuthChallengeDisposition.useCredential, nil);
  100. }
  101. }
  102. public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  103. print("didStartProvisionalNavigation");
  104. }
  105. public func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
  106. print("didReceiveServerRedirectForProvisionalNavigation");
  107. }
  108. public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  109. NCUtility.sharedInstance.stopActivityIndicator()
  110. }
  111. }