【问题标题】:iOS - detect when user copy to clipboard - [UIPasteboard generalPasteboard]iOS - 检测用户何时复制到剪贴板 - [UIPasteboard generalPasteboard]
【发布时间】:2012-01-20 13:03:03
【问题描述】:

快速简单的问题

在使用带有一些文本的 WebView 时 - 用户可以从中选择一个 sn-p 文本 并按下我创建的 UIButton - 运行以下操作:

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

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

什么是合适的监听器?

【问题讨论】:

标签: iphone objective-c ios copy-paste uipasteboard


【解决方案1】:

使用 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];

【讨论】:

  • 嘿,当用户在任何其他应用程序中复制任何东西时它是否工作。
  • Swift 4 版本怎么样?
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 2010-12-01
    • 1970-01-01
    • 2015-08-27
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多