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

wfauudbj  于 2023-05-19  发布在  iOS
关注(0)|答案(1)|浏览(308)

我想知道是否可以通过私有或公共框架以编程方式检测USB何时连接到iPhone设备。
我知道我们可以通过使用UIDeviceBatteryState检测到这一点,但在这种情况下,它只会检测充电,拔下,或不充电状态,将无法识别其充电是否通过USB连接通过电源直接或通过任何其他设备,如mac或任何其他机器.
请告诉我。

hlswsv35

hlswsv351#

你可以像这样检测它。无需私有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);

相关问题