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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With