Objective C 回调失败
的代码
IONotificationPortRef notificationPortRef = IONotificationPortCreate(kIOMasterPortDefault);
if (!notificationPortRef)
{
return nil;
}
CFRunLoopSourceRef notificationRunLoopSource = IONotificationPortGetRunLoopSource(notificationPortRef);
if (!notificationRunLoopSource)
{
CFRelease(notificationPortRef);
return nil;
}
CFRunLoopAddSource(CFRunLoopGetCurrent(), notificationRunLoopSource, kCFRunLoopDefaultMode);
CFDictionaryRef matching = IOServiceMatching (kIOUSBDeviceClassName);
if (!matching)
{
CFRelease(notificationPortRef);
return nil;
}
io_iterator_t matchingIterator;
kern_return_t result = IOServiceAddMatchingNotification(notificationPortRef,
kIOMatchedNotification,
matching,
&driverNotificationRoutine,
self,
&matchingIterator);
driverNotificationRoutine(self, matchingIterator);
if (result != KERN_SUCCESS)
{
CFRelease(notificationPortRef);
}
CFRunLoopRun();
我有发布通知处理程序和此回调处理程序
static void driverNotificationRoutine (void *refcon, io_iterator_t matchingIterator)
{
NSLog(@"Callback called.");
if (matchingIterator)
{
io_service_t device;
NSInteger count = 0;
while (device = IOIteratorNext(matchingIterator)) count++;
IOObjectRelease(matchingIterator);
if (count)
{
...
}
}
}
,但我的回调函数从未被调用。我用 USB 闪存驱动器尝试过此操作,但没有成功。我哪里错了?
I have the code posting the notification handler
IONotificationPortRef notificationPortRef = IONotificationPortCreate(kIOMasterPortDefault);
if (!notificationPortRef)
{
return nil;
}
CFRunLoopSourceRef notificationRunLoopSource = IONotificationPortGetRunLoopSource(notificationPortRef);
if (!notificationRunLoopSource)
{
CFRelease(notificationPortRef);
return nil;
}
CFRunLoopAddSource(CFRunLoopGetCurrent(), notificationRunLoopSource, kCFRunLoopDefaultMode);
CFDictionaryRef matching = IOServiceMatching (kIOUSBDeviceClassName);
if (!matching)
{
CFRelease(notificationPortRef);
return nil;
}
io_iterator_t matchingIterator;
kern_return_t result = IOServiceAddMatchingNotification(notificationPortRef,
kIOMatchedNotification,
matching,
&driverNotificationRoutine,
self,
&matchingIterator);
driverNotificationRoutine(self, matchingIterator);
if (result != KERN_SUCCESS)
{
CFRelease(notificationPortRef);
}
CFRunLoopRun();
And this callback handler
static void driverNotificationRoutine (void *refcon, io_iterator_t matchingIterator)
{
NSLog(@"Callback called.");
if (matchingIterator)
{
io_service_t device;
NSInteger count = 0;
while (device = IOIteratorNext(matchingIterator)) count++;
IOObjectRelease(matchingIterator);
if (count)
{
...
}
}
}
but my callback function is never called. I tried this with USB flash drive with no success. Where I am wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

不应该有。
回调函数中
There should not be
in the callback function.