Tenho um iOS
aplicativo no qual estou tentando colar algo copiado anteriormente no arquivo .ms do usuário UIPasteboard
. Descobri UIPasteControl
que há uma opção para o usuário tocar para colar silenciosamente sem que o prompt "Permitir Colar" apareça.
Por algum motivo, apesar de ter o que aparentemente são as configurações corretas para o UIPasteControl, ao testar um toque, nada é chamado. Eu esperava override func paste(itemProviders: [NSItemProvider])
que disparasse, mas não dispara.
Qualquer ajuda seria apreciada, pois não parece haver muita informação em lugar nenhum sobre isso UIPasteControl
.
import UIKit
import UniformTypeIdentifiers
class ViewController: UIViewController {
private let pasteControl = UIPasteControl()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
pasteControl.target = self
pasteConfiguration = UIPasteConfiguration(acceptableTypeIdentifiers: [
UTType.text.identifier,
UTType.url.identifier,
UTType.plainText.identifier
])
//Tried setting pasteControl's pastConfiguration, and got the same result.
//pasteControl.pasteConfiguration = UIPasteConfiguration(
//acceptableTypeIdentifiers: [
//UTType.text.identifier,
//UTType.url.identifier,
//UTType.plainText.identifier
//]
//)
view.addSubview(pasteControl)
pasteControl.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
pasteControl.centerXAnchor.constraint(equalTo: view.centerXAnchor),
pasteControl.centerYAnchor.constraint(equalTo: view.centerYAnchor),
])
}
}
extension ViewController {
override func paste(itemProviders: [NSItemProvider]) {
for provider in itemProviders {
if provider.hasItemConformingToTypeIdentifier(UTType.url.identifier) {
provider.loadObject(ofClass: URL.self) { [weak self] reading, _ in
guard let url = reading as? URL else { return }
print(url)
}
}
else if provider.hasItemConformingToTypeIdentifier(UTType.plainText.identifier) {
provider.loadObject(ofClass: NSString.self) { [weak self] reading, _ in
guard let nsstr = reading as? NSString else { return }
let str = nsstr as String
if let url = URL(string: str) {
print(url)
}
}
}
}
}
}