检测USB是否已连接到iPhone设备

Jas*_*eet 0 usb iphone-privateapi ios

我想知道是否有可能检测到何时通过私有或公共框架将USB以编程方式连接到iPhone设备。

我知道我们可以使用UIDeviceBatteryState进行检测,但是在那种情况下,它将仅检测充电,拔出或不充电状态,并且将无法识别其是通过USB充电还是通过电源直接连接或通过任何其他设备(如Mac或任何其他机器。

请告诉我。

cre*_*ker 5

您可以像这样检测到它。无需专用API

static void _callback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
    if ([(__bridge NSString*)name isEqualToString:@"com.apple.mobile.lockdown.host_attached"])
    {
        NSLog(@"USB connected");
    }
    else if ([(__bridge NSString*)name isEqualToString:@"com.apple.mobile.lockdown.host_detached"])
    {
        NSLog(@"USB disconnected");
    }
}

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, _callback, CFSTR("com.apple.mobile.lockdown.host_attached"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, _callback, CFSTR("com.apple.mobile.lockdown.host_detached"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
Run Code Online (Sandbox Code Playgroud)