我正在製作Mac應用程序,並且我需要一個檢測iPhone何時插入的API。謝謝。需要一個檢測iPhone何時插入的API
編輯:爲了澄清,具體而言,我需要一個API來檢測iPhone何時插入Mac上的USB端口。
我正在製作Mac應用程序,並且我需要一個檢測iPhone何時插入的API。謝謝。需要一個檢測iPhone何時插入的API
編輯:爲了澄清,具體而言,我需要一個API來檢測iPhone何時插入Mac上的USB端口。
我沒有一個完整的答案,但一個程序,實現你想要的是USB Prober,隨Xcode一起提供,並位於/Developer/Applications/Utilities/USB Prober.app
。該程序是開源的,瀏覽器可查看的存儲庫爲here,整個項目包含在this download中。我實際上發現一個更老的版本更有幫助,如可用here,特別是BusProbeClass。
它們都依賴於IOKit,Apple的文檔在數量和質量上似乎都很缺乏。
這是沉重的閱讀,但如果你看看+ USBProbe
然後你會看到它得到當前USB設備的列表,獲取IOUSBDeviceInterface
S表示每個變量deviceIntf
,然後將它們推送到某處程序的受用。如果您在同一個源文件中檢出+ outputDevice: locationID:deviceNumber:
,則會看到GetDescriptor
看起來可以在IOUSBDeviceDescriptor
上使用,以獲取包含供應商和產品ID的屬性,這些屬性的組合由USB Implementer論壇保證是唯一的。
使用供應商和產品ID,您可以搜索任何特定的USB設備。從我的Mac系統信息中,我可以告訴你,iPhone 4的產品ID是0x1297,Apple的供應商ID是0x05ac。
另外:從解剖代碼,如果你刪除了一堆檢查,事情是成功的,並將它壓縮到演示的東西,那麼下面至少是測試一個iPhone 4是否插入了現在你需要鏈接到基金會和由於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;
}
我還沒有想出如何觀察在插入式設備的變化。
這正是我正在尋找的 –
約翰,你能澄清你的問題嗎?據我所知,你想寫一個MAC OS X的應用程序 - 對嗎?如果是這樣,請記住@ Scott的代碼適用於iOS,不適用於OSX。此外,他的代碼不包括Mac不是充電設備的情況......它可以是PC或其他USB主機......更不用說牆上適配器通過USB電纜給iPhone充電...... – matm
是的,我是爲OS X編寫應用程序,我需要一個API來檢測iDevice何時插入Mac。我的意思是'f0recast'應用程序的一個很好的例子,我一直沒有找到任何好的答案。 – John