2017-10-23 23 views
0

私は、USBがiPhoneデバイスにプログラムまたはプライベートまたはパブリックフレームワークを介して接続されているかどうかを知りたいと思っています。USBがiPhoneデバイスに接続されているかどうかを検出する

私はUIDeviceBatteryStateを使用してこれを検出することができますが、その場合は充電中、電源が切れていない、または充電中でないことを検出し、USB経由で直接または電源経由で充電されたかどうかを特定できませんmacや他のマシンのような他のデバイス。

お知らせください。

+0

助けてください –

答えて

2

このように検出できます。プライベート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); 
関連する問題