需要一个检测iPhone何时插入的API

我为Mac制作了一个应用程序,而且我需要一个检测iPhone何时插入的API。谢谢。

编辑:为了澄清,具体来说,我需要一个API,检测到一个iPhone插入到Mac上的USB端口。

我没有一个完整的答案,但一个程序,实现你想要的是USB Prober,随Xcode一起提供,位于/Developer/Applications/Utilities/USB Prober.app 。 该程序是开源的,浏览器可查看的存储库在这里 ,整个项目都包含在这个下载中 。 实际上,我发现一个更老的版本更有帮助,如这里可用,特别是BusProbeClass 。

他们都依赖于苹果的文档在数量和质量上都非常缺乏的IOKit。

这是繁重的阅读,但如果你签出+ USBProbe那么你会看到它获得当前USB设备的列表,获取variablesdeviceIntf每个deviceIntf ,然后将它们推到某个地方有用的程序的其余部分。 如果在相同的源文件中检出+ outputDevice: locationID:deviceNumber: ,则会看到GetDescriptor看起来可以在IOUSBDeviceDescriptor上使用,以获取包括供应商和产品ID的属性,其组合保证是由USB实施者论坛独一无二的。

使用供应商和产品ID,您可以search任何特定的USB设备。 从我的Mac系统信息中,我可以告诉你,iPhone 4的产品ID是0x1297,而Apple的供应商ID是0x05ac。

额外:从解剖代码,如果你删除了一大堆的检查,成功的东西,并压缩下来,以示范的东西,那么以下至less是一个testingiPhone 4是否插入现在(你需要链接到Foundation和IOKit框架):

 #include <stdio.h> #import <Foundation/Foundation.h> #import <IOKit/usb/IOUSBLib.h> #import <IOKit/IOCFPlugIn.h> #import <mach/mach_port.h> int main (int argc, const char * argv[]) { // get the port through which to talk to the kernel mach_port_t masterDevicePort; IOMasterPort(MACH_PORT_NULL, &masterDevicePort); // create a dictionary that describes a search // for services provided by USB CFDictionaryRef matchingDictionary = IOServiceMatching(kIOUSBDeviceClassName); // get an iterator for all devices that match // the dictionary io_iterator_t deviceIterator; IOServiceGetMatchingServices( masterDevicePort, matchingDictionary, &deviceIterator); // iterate through the iterator... io_service_t ioDevice; while((ioDevice = IOIteratorNext(deviceIterator))) { IOUSBDeviceInterface **deviceInterface = NULL; IOCFPlugInInterface **ioPlugin = NULL; SInt32 score; // get a pointer to the device, stored to ioPlugin IOCreatePlugInInterfaceForService( ioDevice, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &ioPlugin, &score); // ask the device for its interface (*ioPlugin)->QueryInterface( ioPlugin, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (void *)&deviceInterface); // make and issue a request to get the device descriptor IOUSBDeviceDescriptor deviceDescriptor; IOUSBDevRequest request; request.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice); request.bRequest = kUSBRqGetDescriptor; request.wValue = kUSBDeviceDesc << 8; request.wIndex = 0; request.wLength = sizeof(deviceDescriptor); request.pData = &deviceDescriptor; (*deviceInterface)->DeviceRequest(deviceInterface, &request); // now we have the device descriptor, do a little cleaning up - // release the interface and the device (*deviceInterface)->Release(deviceInterface); IOObjectRelease(ioDevice); // ensure that the values returned are in the appropriate // byte order for this platform CFSwapInt16LittleToHost(deviceDescriptor.idVendor); CFSwapInt16LittleToHost(deviceDescriptor.idProduct); // check whether we have an iPhone 4 attached if(deviceDescriptor.idVendor == 0x05ac && deviceDescriptor.idProduct == 0x1297) printf("iPhone 4 is connected!"); } // clean up by releasing the device iterator // and returning the communications port IOObjectRelease(deviceIterator); mach_port_deallocate(mach_task_self(), masterDevicePort); return 0; } 

我还没有想出如何观察插入式设备的变化。