我订阅了剪贴板文字更改通知。每次在我的应用程序中将某些内容复制到剪贴板中时,它都会触发该事件两次。换句话说,onCopy()被调用两次。 (xCode 8.1; iOS 9,10)
import Foundation
protocol Clipboard {
func onCopy()
}
class SecureClipboard : NSObject, Clipboard {
static let sharedReader = SecureClipboard()
private var clipboardContent: String?
private var clipboardBeingCleared: Bool = false
/// Lifycycle
private override init() {
super.init()
subscribForClipboardChanges()
}
private func subscribForClipboardChanges() {
NotificationCenter.default.addObserver(self,
selector: #selector(onCopy),
name:NSNotification.Name.UIPasteboardChanged,
object: nil)
}
/// Clipboard
func onCopy() {
// Called twice
}
}