1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
//
// NCViewerRichWorkspaceWebView.swift
// Nextcloud
//
// Created by Marino Faggiana on 12/12/19.
// Copyright © 2019 Marino Faggiana. All rights reserved.
//
// Author Marino Faggiana <marino.faggiana@nextcloud.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
import UIKit
import WebKit
class NCViewerRichWorkspaceWebView: UIViewController, WKNavigationDelegate, WKScriptMessageHandler {
@IBOutlet weak var webView: WKWebView!
@IBOutlet weak var webViewBottomConstraint: NSLayoutConstraint!
@objc var metadata: tableMetadata?
@objc var url: String = ""
// MARK: - View Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
let userAgent: String = CCUtility.getUserAgent()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
var request = URLRequest(url: URL(string: url)!)
request.addValue("true", forHTTPHeaderField: "OCS-APIRequest")
let language = NSLocale.preferredLanguages[0] as String
request.addValue(language, forHTTPHeaderField: "Accept-Language")
webView.configuration.userContentController.add(self, name: "DirectEditingMobileInterface")
webView.navigationDelegate = self
webView.customUserAgent = userAgent
webView.load(request)
}
@objc func keyboardDidShow(notification: Notification) {
let safeAreaInsetsBottom = UIApplication.shared.keyWindow!.safeAreaInsets.bottom
guard let info = notification.userInfo else { return }
guard let frameInfo = info[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
let keyboardFrame = frameInfo.cgRectValue
webViewBottomConstraint.constant = keyboardFrame.size.height - safeAreaInsetsBottom
}
@objc func keyboardWillHide(notification: Notification) {
webViewBottomConstraint.constant = 0
}
// MARK: -
public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == "DirectEditingMobileInterface" {
if message.body as? String == "close" {
self.presentationController?.delegate?.presentationControllerWillDismiss?(self.presentationController!)
dismiss(animated: true) {
NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterCloseRichWorkspaceWebView, userInfo: nil)
}
}
if message.body as? String == "share" {
if metadata != nil {
NCFunctionCenter.shared.openShare(viewController: self, metadata: metadata!, indexPage: .sharing)
}
}
if message.body as? String == "loading" {
print("loading")
}
if message.body as? String == "loaded" {
print("loaded")
}
if message.body as? String == "paste" {
self.paste(self)
}
}
}
// MARK: -
public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
if let serverTrust = challenge.protectionSpace.serverTrust {
completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
} else {
completionHandler(URLSession.AuthChallengeDisposition.useCredential, nil)
}
}
public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("didStartProvisionalNavigation")
}
public func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
print("didReceiveServerRedirectForProvisionalNavigation")
}
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("didFinish")
}
}
|