Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIPasteboard无法多次复制过期的文本_Ios_Swift_Clipboard_Uipasteboard_Pasteboard - Fatal编程技术网

Ios UIPasteboard无法多次复制过期的文本

Ios UIPasteboard无法多次复制过期的文本,ios,swift,clipboard,uipasteboard,pasteboard,Ios,Swift,Clipboard,Uipasteboard,Pasteboard,我打电话: UIPasteboard.general.setItems([[kUTTypePlainText as String: text]], options: [.localOnly: true, .expirationDate: expirationTime]) 要复制每个按钮的文本,请单击。但是,过期时间过后(30秒),复制功能将停止工作。在调试器中查找后,第二次(或之后)调用此行时,UIPasteboard中的items数组返回为空。为什么会这样?其他应用程序,如Lastpass,

我打电话:

UIPasteboard.general.setItems([[kUTTypePlainText as String: text]], options: [.localOnly: true, .expirationDate: expirationTime])
要复制每个按钮的文本,请单击。但是,过期时间过后(30秒),复制功能将停止工作。在调试器中查找后,第二次(或之后)调用此行时,
UIPasteboard
中的
items
数组返回为空。为什么会这样?其他应用程序,如Lastpass,允许文本在过期时间内复制多次


我有预感,这可能与正在使用的钥匙有关,有什么想法吗

在花费了太多时间之后,我无法理解为什么
setItems(u:options:)
中的
expirationDate
选项对于该函数的后续使用不起作用。这方面没有任何其他文档。这要么是一个我无法理解的基本问题,要么是API中更复杂的问题

无论如何,我已经用定时器实现了这个解决方案。这将适用于所有iOS版本,我们只需在30秒后清除UIPasteboard的items数组。
expirationDate
选项仅适用于iOS 10.3及以上版本,而此功能更强大,适用于所有版本

class MyPasteboard {
    private static let shared = MyPasteboard()
    private let pasteboard = UIPasteboard.general
    private var expirationTimer: Timer?

    private init() {}

    static func copyText(_ text: String, expirationTime: TimeInterval = 30) {
        shared.pasteboard.string = text
        shared.expirationTimer?.invalidate()
        shared.expirationTimer = Timer.scheduledTimer(
            timeInterval: expirationTime,
            target: self,
            selector: #selector(expireText),
            userInfo: nil,
            repeats: false
        )
    }

    @objc private static func expireText() {
        shared.expirationTimer = nil
        shared.pasteboard.items = []
    }
}

有许多不同的方法来构建它,但我选择这样做,因为它允许我抽象出UIPasteboard复制功能,并通过静态功能(这是我所需要的)进行简单使用

在花费了太多时间之后,我无法理解为什么
setItems(u:options:)
中的
expirationDate
选项对于该函数的后续使用不起作用。这方面没有任何其他文档。这要么是一个我无法理解的基本问题,要么是API中更复杂的问题

无论如何,我已经用定时器实现了这个解决方案。这将适用于所有iOS版本,我们只需在30秒后清除UIPasteboard的items数组。
expirationDate
选项仅适用于iOS 10.3及以上版本,而此功能更强大,适用于所有版本

class MyPasteboard {
    private static let shared = MyPasteboard()
    private let pasteboard = UIPasteboard.general
    private var expirationTimer: Timer?

    private init() {}

    static func copyText(_ text: String, expirationTime: TimeInterval = 30) {
        shared.pasteboard.string = text
        shared.expirationTimer?.invalidate()
        shared.expirationTimer = Timer.scheduledTimer(
            timeInterval: expirationTime,
            target: self,
            selector: #selector(expireText),
            userInfo: nil,
            repeats: false
        )
    }

    @objc private static func expireText() {
        shared.expirationTimer = nil
        shared.pasteboard.items = []
    }
}
有许多不同的方法来构建它,但我选择这样做,因为它允许我抽象出UIPasteboard复制功能,并通过静态功能(这是我所需要的)进行简单使用