Developer Documentations doesn't provides fully describe of registerForDraggedTypes method. For example, i want that my app allow access only "*.abc" files. How can i do this?
A:
If you'd like to have files dragged onto your view, your should register for the NSFilenamesPboardType type. If you want accept only certain filenames, you can do that in your implementation of performDragOperation:. Something like:
- (BOOL)performDragOperation:(id < NSDraggingInfo >)sender {
NSArray *draggedFilenames = [[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType];
if ([[[draggedFilenames objectAtIndex:0] pathExtension] isEqual:@"abc"])
return YES;
else
return NO;
}
Tom
2010-04-09 03:26:51
Looking only at the first path is bad practice, as it makes for inconsistent behavior when the user drags multiple files. If the first one is a .abc file, you return `YES` regardless of whether any others are; conversely, if the first one is not a .abc file, you return `NO` even if there is a .abc file among the other files. Also, you're comparing case-sensitively; you won't match .ABC files, for example. Better would be to loop through the `pathExtension` s and use `caseInsensitiveCompare:` to find .abc files (and decide whether you want to return `YES` if any non-.abc files are in the drag).
Peter Hosey
2010-04-09 03:49:37
Yep. It's also a bad idea to use path extensions to decide which files to handle; better to use Uniform Type Identifiers. This snippet is just a starting point.
Tom
2010-04-09 04:12:17