NCRichdocument.swift 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // NCRichdocument.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 06/09/18.
  6. // Copyright © 2018 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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 NCRichdocument: NSObject, WKNavigationDelegate, WKScriptMessageHandler {
  25. @objc static let sharedInstance: NCRichdocument = {
  26. let instance = NCRichdocument()
  27. return instance
  28. }()
  29. var viewDetail: CCDetail!
  30. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  31. @objc func viewRichDocumentAt(_ link: String, viewDetail: CCDetail) {
  32. self.viewDetail = viewDetail
  33. let contentController = WKUserContentController()
  34. contentController.add(self, name: "RichDocumentsMobileInterface")
  35. let configuration = WKWebViewConfiguration()
  36. configuration.userContentController = contentController
  37. let webView = WKWebView(frame: viewDetail.view.bounds, configuration: configuration)
  38. webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  39. webView.navigationDelegate = self
  40. var request = URLRequest(url: URL(string: link)!)
  41. request.addValue("true", forHTTPHeaderField: "OCS-APIRequest")
  42. let language = NSLocale.preferredLanguages[0] as String
  43. request.addValue(language, forHTTPHeaderField: "Accept-Language")
  44. let userAgent : String = CCUtility.getUserAgent()
  45. webView.customUserAgent = userAgent
  46. webView.load(request)
  47. viewDetail.view.addSubview(webView)
  48. }
  49. public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
  50. if (message.name == "RichDocumentsMobileInterface") {
  51. if message.body as! String == "close" {
  52. print("\(message.body)")
  53. }
  54. }
  55. }
  56. public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
  57. if let serverTrust = challenge.protectionSpace.serverTrust {
  58. completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
  59. } else {
  60. completionHandler(URLSession.AuthChallengeDisposition.useCredential, nil);
  61. }
  62. }
  63. public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  64. print("didStartProvisionalNavigation");
  65. }
  66. public func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
  67. print("didReceiveServerRedirectForProvisionalNavigation");
  68. }
  69. public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  70. print("didFinish");
  71. }
  72. }