需要一个API来检测iPhone何时插入
我正在为Mac制作一个应用程序,我需要一个API来检测iPhone何时插入。谢谢需要一个API来检测iPhone何时插入,iphone,objective-c,ios,macos,Iphone,Objective C,Ios,Macos,我正在为Mac制作一个应用程序,我需要一个API来检测iPhone何时插入。谢谢 编辑:为了澄清这一点,我需要一个API来检测iPhone何时插入Mac上的USB端口 我没有一个完整的答案,但是一个可以实现您想要的功能的程序是USB Prober,它与Xcode一起提供,位于/Developer/Applications/Utilities/USB Prober.app。该程序是开源的,浏览器可查看的存储库和整个项目都包含在其中。事实上,我发现一个旧版本更有用,尤其是在可用的情况下 它们都依赖于
编辑:为了澄清这一点,我需要一个API来检测iPhone何时插入Mac上的USB端口 我没有一个完整的答案,但是一个可以实现您想要的功能的程序是USB Prober,它与Xcode一起提供,位于
/Developer/Applications/Utilities/USB Prober.app
。该程序是开源的,浏览器可查看的存储库和整个项目都包含在其中。事实上,我发现一个旧版本更有用,尤其是在可用的情况下
它们都依赖于IOKit,苹果的文档在数量和质量上似乎都非常缺乏
这是一个繁重的阅读,但是如果您签出+USBProbe
,您将看到它获得当前USB设备的列表,为变量deviceIntf
中的每个设备获取ioubdeviceinterface
s,然后将它们推送到对程序其余部分有用的地方。如果您在同一源文件的下方签出+outputDevice:locationID:deviceNumber:
,您将看到GetDescriptor
似乎可以在IobDevicedDescriptor
上使用,以获取包括供应商和产品ID在内的属性,USB实施者论坛保证其组合是唯一的
使用供应商和产品ID,您可以搜索任何特定的USB设备。从我的Mac电脑的系统信息中,我可以告诉你iPhone4的产品ID为0x1297,苹果的供应商ID为0x05ac
Ext:从剖析该代码,如果你删除了一组检查成功并将其压缩到演示性内容的检查,那么下面至少是对iPhone 4是否正在插入的测试(你需要链接到基础和IOKit框架):
#包括
#进口
#进口
#进口
#进口
int main(int argc,const char*argv[]
{
//获取与内核通信的端口
mach_port_t masterDevicePort;
IOMasterPort(MACH_PORT_NULL和masterDevicePort);
//创建描述搜索的词典
//对于USB提供的服务
CFDictionaryRef matchingDictionary=IOServiceMatching(kIOUSBDeviceClassName);
//为匹配的所有设备获取迭代器
//字典
io迭代器设备迭代器;
IOServiceGetMatchingServices(
masterDevicePort,
匹配字典,
&设备计数器);
//遍历迭代器。。。
io_服务碘设备;
while((ioDevice=IOIteratorNext(deviceIterator)))
{
IOUSBDeviceInterface**deviceInterface=NULL;
IOCFPlugInInterface**ioPlugin=NULL;
32分;
//获取指向设备的指针,该指针存储在ioPlugin中
IOCreatePlugInInterfaceForService(
ioDevice,
kIOUSBDeviceUserClientTypeID,
Kiocfplugineid,
&伊普卢金,
&分数);
//询问设备的接口
(*ioPlugin)->查询接口(
伊普卢金,
cfuUIDGetuUIBytes(kIOUSBDeviceInterfaceID),
(无效*)和设备接口);
//发出请求以获取设备描述符
b副描述器设备描述器;
请求请求;
request.bmRequestType=usbmakebrequesttype(kUSBIn、kUSBStandard、kUSBDevice);
request.bRequest=kUSBRqGetDescriptor;
request.wValue=kUSBDeviceDesc DeviceRequest(设备接口和请求);
//现在我们有了设备描述符,做一些清理-
//释放接口和设备
(*deviceInterface)->发布(deviceInterface);
IOObjectRelease(ioDevice);
//确保返回的值位于适当的位置
//此平台的字节顺序
CFSwapInt16LittleToHost(deviceDescriptor.idVendor);
CFSwapInt16LittleToHost(deviceDescriptor.idProduct);
//检查我们是否连接了iPhone4
if(deviceDescriptor.idVendor==0x05ac&&deviceDescriptor.idProduct==0x1297)
printf(“iPhone4已连接!”);
}
//通过释放设备迭代器进行清理
//并返回通讯端口
IOObjectRelease(设备计数器);
mach_port_deallocate(mach_task_self(),主设备端口);
返回0;
}
我还没有弄明白如何观察插入式设备的变化。John,你能澄清一下你的问题吗?据我所知,你想为MacOSX编写一个应用程序-对吗?如果是这样,请记住@Scott的代码是针对iOS的,而不是针对OSX的。此外,他的代码没有涵盖Mac不是充电设备的情况。。。它可以是PC或其他USB主机。。。更不用说通过USB电缆为iPhone充电的墙壁适配器了……是的,我正在为OS X编写一个应用程序,我需要一个API来检测iDevice何时插入Mac。我想说的一个很好的例子是“f0recast”应用程序,我一直没有找到任何好的答案。这正是我想要的
#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;
}