我有发布通知处理程序的代码
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闪存驱动器但没有成功。哪里错了?
答案 0 :(得分:1)
应该没有
IOObjectRelease(matchingIterator);
在回调函数中。