NCRichdocument.swift 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 webView: WKWebView?
  30. var request: URLRequest!
  31. var viewDetail: CCDetail?
  32. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  33. @objc func viewRichDocumentAt(_ link: String, viewDetail: CCDetail) {
  34. self.viewDetail = viewDetail
  35. let contentController = WKUserContentController()
  36. contentController.add(self, name: "RichDocumentsMobileInterface")
  37. let configuration = WKWebViewConfiguration()
  38. configuration.userContentController = contentController
  39. webView = WKWebView(frame: viewDetail.view.bounds, configuration: configuration)
  40. webView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  41. webView?.navigationDelegate = self
  42. self.request = URLRequest(url: URL(string: link)!)
  43. self.request.addValue("true", forHTTPHeaderField: "OCS-APIRequest")
  44. let language = NSLocale.preferredLanguages[0] as String
  45. self.request.addValue(language, forHTTPHeaderField: "Accept-Language")
  46. let userAgent : String = CCUtility.getUserAgent()
  47. webView!.customUserAgent = userAgent
  48. webView!.load(self.request!)
  49. viewDetail.view.addSubview(webView!)
  50. }
  51. public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
  52. print("WKScriptMessage");
  53. }
  54. public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
  55. if let serverTrust = challenge.protectionSpace.serverTrust {
  56. completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
  57. } else {
  58. completionHandler(URLSession.AuthChallengeDisposition.useCredential, nil);
  59. }
  60. }
  61. public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  62. print("didStartProvisionalNavigation");
  63. }
  64. public func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
  65. print("didReceiveServerRedirectForProvisionalNavigation");
  66. }
  67. public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  68. print("didFinish");
  69. }
  70. }