Drag and Drop - Is it possible to get the URL?

落花浮王杯 提交于 2020-01-02 04:55:09

问题


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

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