Skip to content

Commit 06c8d5c

Browse files
committed
workaround pasteboard inaccessible without full access
1 parent 9732544 commit 06c8d5c

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

keyboard/KeyboardViewController.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class KeyboardViewController: UIInputViewController, FcitxProtocol {
2626
var id: UInt64!
2727
var hostingController: UIHostingController<VirtualKeyboardView>!
2828
var removedBySlide = ""
29+
static private var clipboardText = ""
2930

3031
private func updateTextIsEmpty() {
3132
let text =
@@ -189,23 +190,34 @@ class KeyboardViewController: UIInputViewController, FcitxProtocol {
189190
textDocumentProxy.setMarkedText(preedit, selectedRange: NSRange(location: caret, length: 0))
190191
}
191192

193+
private func writeToClipboard(_ text: String) {
194+
if hasFullAccess {
195+
// On real device, this fails silently if full access is not granted. Simulator works which is misleading.
196+
UIPasteboard.general.string = text
197+
}
198+
KeyboardViewController.clipboardText = text
199+
}
200+
192201
public func cut() {
193202
if let text = textDocumentProxy.selectedText {
194-
UIPasteboard.general.string = text
203+
writeToClipboard(text)
195204
textDocumentProxy.deleteBackward()
196205
updateTextIsEmpty()
197206
}
198207
}
199208

200209
public func copy() {
201210
if let text = textDocumentProxy.selectedText {
202-
UIPasteboard.general.string = text
211+
writeToClipboard(text)
203212
}
204213
}
205214

206215
public func paste() {
207216
if let text = UIPasteboard.general.string {
208217
commitString(text)
218+
// No need to store it, as when user turns off full access, system restarts keyboard.
219+
} else if !KeyboardViewController.clipboardText.isEmpty {
220+
commitString(KeyboardViewController.clipboardText)
209221
}
210222
}
211223

0 commit comments

Comments
 (0)