Search code examples
objective-cxcodemacoscocoadrag-and-drop

Drag&Drop NSURL without "file://"


I'm implementing my drag&drop method. I need that when user drags something on my app window I can get that file URL. NSURL needs to be converted to char. Thats OK. But how to remove file:// from url? My current code:

pboard = [sender draggingPasteboard];
NSString *url = [[NSURL URLFromPasteboard:pboard] absoluteString];
input_imageN = strdup([url UTF8String]);

its OK, but it gives url with file:// prefix. I tried using

NSURL *fileUrl = [[NSURL URLFromPasteboard:pboard] isFileURL];
NSString *url = [fileUrl absoluteString];
NSLog(@"url: %@", [NSURL URLFromPasteboard:pboard]);
input_imageN = strdup([url UTF8String]);

but it says that

Cannot initialize a variable of type 'NSURL *' with an rvalue of type 'BOOL' (aka 'signed char')

at

NSURL *fileUrl = [[NSURL URLFromPasteboard:pboard] isFileURL];

Solution

  • url = [url stringByReplacingOccurencesOfString:@"file://" withString:@""];