iOS - detect when user copy to clipboard - [UIPasteboard generalPasteboard]

岁酱吖の 提交于 2019-12-04 11:59:41

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/8941860/ios-detect-when-user-copy-to-clipboard-uipasteboard-generalpasteboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!