Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何检测“允许从其它 App 粘贴”选项是否允许? #19

Open
s1ntoneli opened this issue Jan 23, 2025 · 0 comments
Open
Labels

Comments

@s1ntoneli
Copy link
Owner

一个不靠谱并且要弹一次窗的办法,但是能用:

计算获取 UIPasteboard.general.string 花了多长时间。

如果已经允许,那么通常在16ms,也就是一帧的时间内完成。有时候也两帧三帧。
而如果是询问,则通常需要1s或更久。

所以,通过这个时间差,就可以猜测出是否已经授权。

办法是土办法,但是能用,取决于你想要什么样的交互。

// 抽象检测方法
private func checkPastePermission() -> Bool {
    let startTime = DispatchTime.now()
    _ = UIPasteboard.general.string
    let milliseconds = Double(DispatchTime.now().uptimeNanoseconds - startTime.uptimeNanoseconds) / 1_000_000
    
    print("剪贴板访问耗时: \(String(format: "%.2f", milliseconds))ms")
    // 16*5 5帧
    return milliseconds < 16 * 5
}
@s1ntoneli s1ntoneli added the iOS label Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant