Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
Hello!
How can I detect in code if a USB storage device is connected to the computer? I need to be able to move files off of the device and move files on the device. I looked at IOKit, but the App Documentation said it was used for writing device drivers, which is not what I want to do. It would be helpful if someone could point me to some documentation about the task I would like to achieve. This will be done in Objective-C.
Thanks!
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,425
A sea of green
A USB storage device should appear as a removable disk. So hook into the mechanisms for handling disk mount events, and for working with removable disks.

Start with the NSWorkspace class, and look at the notifications related to disk mounts: NSWorkspaceDidMountNotification etc.

Also see the DiskArbitration framework, the hdiutil command-line tool, and the FSMegaInfo sample code.
 

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
Is there anything higher level than DiskArbitration? It seems to be all in C and it's very confusing..
Thanks!
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,425
A sea of green
Is there anything higher level than DiskArbitration? It seems to be all in C and it's very confusing..
Thanks!

NSWorkspace is higher-level. I did say you should start there.

I also specifically gave the name of a notification which indicates that a disk was mounted. There are other methods in NSWorkspace for getting removable disks, which a USB storage device is almost certainly going to fall into.

If you already read NSWorkspace's class docs and didn't find a feature you need, then post exactly what feature you need. Otherwise start with the NSWorkspace class.
 

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
I am also interested in discovery attributes about the USB device. Such as its ID
 

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
Just general information about the device such as its manufacturer and available space.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,425
A sea of green
Manufacturer is the USB Vendor ID. You'll need IOKit to get it.

If the programming docs are too difficult, see the 'ioreg' command's man page.

You can get the source for 'ioreg' from Apple's open source page:
http://www.opensource.apple.com/

It's under IOKitTools for a particular OS release, e.g.:
http://www.opensource.apple.com/source/IOKitTools/IOKitTools-78/


"Available space" is ambiguous. Do you mean total space of the storage device, or free space after subtracting space used by current files? Either way, you can get that using the statfs() function or getattrlist(), both C functions, which have man pages. Or see the FSMegaInfo sample code I previously linked to.

You can also use NSTask (an Obj-C class) or popen() (a C function) to run the command:
Code:
diskutil info -plist /path/of/usb-disk-here
The path of the disk is provided as one of the values in the disk-mount event (see NSWorkspace docs previously referenced).
 

Sydde

macrumors 68030
Aug 17, 2009
2,552
7,050
IOKWARDI
Use the NSFileManager method -attributesOfFileSystemForPath:error: to get size and free space on a drive.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.