iOS - 检测用户何时复制到剪贴板 - [UIPasteboard genericPasteboard]

发布于 2024-12-28 07:47:22 字数 343 浏览 4 评论 0原文

使用带有一些文本的 WebView 时快速简单的问题

- 用户可以从中选择文本片段 并按我创建的 UIButton - 运行以下操作:

-(IBAction)copyToClip
{
    NSString *copyClip = [UIPasteboard generalPasteboard].string;
    NSLog(@"Clip = %@",copyClip);
    // (works fine)
}

我想在没有 UIButton 的情况下调用相同的函数,因此当用户执行“复制”操作时,它将激活上述代码。 (我假设有一个听众)

什么是合适的听众?

quick easy question

while using a WebView with some text in it - the user can select a snippet of text from it
and press a UIButton which I created - running the following action:

-(IBAction)copyToClip
{
    NSString *copyClip = [UIPasteboard generalPasteboard].string;
    NSLog(@"Clip = %@",copyClip);
    // (works fine)
}

I would like to call the same function without a UIButton, thus when the user did a "copy" action it will activate the above code. (I assume a listener)

what would be the appropriate listener for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木緿 2025-01-04 07:47:22

使用 NSNotificationCenter 并注册 UIPasteboardChangedNotification:
http://developer.apple.com/library/IOs/documentation/UIKit/Reference/UIPasteboard_Class/Reference.html#//apple_ref/c/data/UIPasteboardChangedNotification

[[NSNotificationCenter defaultCenter] addObserver:object selector:@selector(copyToClip) name:UIPasteboardChangedNotification object:nil];

Use NSNotificationCenter and register for UIPasteboardChangedNotification:
http://developer.apple.com/library/IOs/documentation/UIKit/Reference/UIPasteboard_Class/Reference.html#//apple_ref/c/data/UIPasteboardChangedNotification

[[NSNotificationCenter defaultCenter] addObserver:object selector:@selector(copyToClip) name:UIPasteboardChangedNotification object:nil];
起风了 2025-01-04 07:47:22

如果有人对 Xamarin/C# 版本感兴趣:

NSNotificationCenter.DefaultCenter.AddObserver(UIPasteboard.ChangedNotification, 
            notification => { 
                // custom code here
            });

If someone is interested in the Xamarin/C# version:

NSNotificationCenter.DefaultCenter.AddObserver(UIPasteboard.ChangedNotification, 
            notification => { 
                // custom code here
            });
巨坚强 2025-01-04 07:47:22

Swift 5

UIPasteboard。 changedNotification

NotificationCenter.default.addObserver(self, selector: #selector(handleCopy), name: UIPasteboard.changedNotification, object: nil)

@objc func handleCopy(sender: NSNotification) {
    // Handle new pasteboard
    print("pasteboard changed: \(UIPasteboard.general.items.first)")
}

不要忘记将 @objc 添加到您的处理程序中,以便该方法对 Objective-C 选择器可见。

Swift 5

UIPasteboard.changedNotification

NotificationCenter.default.addObserver(self, selector: #selector(handleCopy), name: UIPasteboard.changedNotification, object: nil)

@objc func handleCopy(sender: NSNotification) {
    // Handle new pasteboard
    print("pasteboard changed: \(UIPasteboard.general.items.first)")
}

Don't forget to add @objc to your handler so the method is visible to the objective-C selector.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文