问题
I implemented a simple drag and drop. The main purpose is for users to drag and drop images from a browser. Is it possible for me get the URL that this image was dragged from?
So lets say, I am on SO and I drag and drop the logo. Is there a way for me to know that this was from http://stackoverflow.com ? Thanks
回答1:
For images which aren't also links, the following code will log the URL a dragged image came from. This works for me in Safari & Firefox.
@implementation DragView
- (void)awakeFromNib {
[self registerForDraggedTypes:[NSArray arrayWithObject:NSURLPboardType]];
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
return NSDragOperationCopy;
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
NSPasteboard *pboard;
pboard = [sender draggingPasteboard];
NSLog(@"types: %@", [pboard types]);
NSLog(@"url: %@", [NSURL URLFromPasteboard:pboard]);
return YES;
}
@end
If the image is also a link, the logged URL is a href of that link. It's also possible to get the "where from" URL from a file (as seen in the Finder's Get Info panel) using the kMDItemWhereFroms key of the extended attributes.
来源:https://stackoverflow.com/questions/10203402/drag-and-drop-is-it-possible-to-get-the-url