Objective C Callback failed
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?
There should not be
IOObjectRelease(matchingIterator);
in the callback function.
精彩评论