开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜