Question

I have a custom NSView that drags a custom core data object. I start the drag process by calling this method:

[self dragImage:graphicItem.dragImage at:point offset:CGSizeZero event:theEvent pasteboard:pb source:self slideBack:YES];

Before calling the method above I grab the general pasteboard and set a title like this:

pb = [NSPasteboard generalPastebaord];
[pb setString:graphicItem.itemDescription forType:(NSString *)kUTTypeText];

but immediately after I set the string if I call:

[pb stringForType:(NSString *)kUTTypeText]

I get nothing.

If I create a pasteboard like this:

pb = [NSPasteboard pasteboardWithName:"myPasteBoard"]

it does work. So, I tried getting another standard pasteboard

pb = [NSPasteboard pasteboardWithName: NSDragPboard];

But this one too, won't let me write data to it.

I'm calling this method from mouseDragged:(NSEvent *)event; My application is sandboxed. Any ideas ?

Was it helpful?

Solution

When using the general pasteboard, you have to clear it's contents to prepare it for new items.
From the docs:

Clears the existing contents of the pasteboard, preparing it for new contents. This is the first step in providing data on the pasteboard.

Apple also provides a string constant for plain text string pasteboard contents:NSPasteboardTypeString

The following code works, and uses the general pasteboard:

NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
[pasteboard setString:@"TestString" forType:NSPasteboardTypeString];
NSLog(@"%@", [pasteboard stringForType:NSPasteboardTypeString]);

OTHER TIPS

Missing before writing to the pasteboard

[pb declareTypes:@[(NSString *)kUTTypeText] owner:self]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top