I need to detect when an USB drive e.g. Office23, is mounted in OS X and also search for a file eg. jan.txt inside the drive and copy it to app bundle.
I am using following method to find if volume is mounted or not:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = error;
NSArray *keys = @[NSURLVolumeNameKey, NSURLVolumeIsRemovableKey, NSURLVolumeIsEjectableKey];
NSArray *volumeURL = [fileManager mountedVolumeURLsIncludingResourceValuesForKeys:keys options:NSVolumeEnumerationProduceFileReferenceURLs];
for (NSURL *url in volumeURL) {
NSLog(@"URL %@", url);
}
Result: file:///.file/id=6571367.2/
How can I get the NSURL of the file inside Volume?
EDIT 1:
NSArray *keys = [NSArray arrayWithObjects:NSURLVolumeNameKey, NSURLVolumeIsRemovableKey, nil];
NSArray *urls = [[NSFileManager defaultManager] mountedVolumeURLsIncludingResourceValuesForKeys:keys options:0];
NSURL *officeDriveURL;
for (NSURL *url in urls) {
NSError *error;
NSNumber *isRemovable;
NSString *volumeName;
[url getResourceValue:&isRemovable forKey:NSURLVolumeIsRemovableKey error:&error];
if ([isRemovable boolValue]) {
[url getResourceValue:&volumeName forKey:NSURLVolumeNameKey error:&error];
if ([[url absoluteString] containsString:@"Office"]) {
officeDriveURL = url;
}
}
}
NSURL *fullURL = [officeDriveURL URLByAppendingPathComponent:@"documents/My File2.txt"];
if ([[NSFileManager defaultManager]fileExistsAtPath:[fullURL absoluteString]]) {
NSLog(@"YES FILE Exist %@", fullURL);
}
Still, FileManager cannot find the url.
The fullURL is: file:///Volumes/Office/documents/My%File2.txt
When I check this through Finder or NSFileManager, this does not not work, but the path is correct.