我想知道是否可以通过私有(private)或公共(public)框架以编程方式检测 USB 何时连接到 iPhone 设备。
我知道我们可以使用 UIDeviceBatteryState 检测到这一点,但在那种情况下,它只会检测充电、拔出或未充电状态,并且无法识别它是通过直接通过电源连接的 USB 充电还是通过任何其他方式充电其他设备,如 mac 或任何其他机器。
请告诉我。
最佳答案
你可以像这样检测它。不需要私有(private) 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);
关于ios - 检测 USB 是否连接到 iPhone 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46883205/