Objective C 回调失败

发布于 11-18 12:22 字数 1753 浏览 5 评论 0原文

的代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

留蓝2024-11-25 12:22:26

不应该有。

IOObjectRelease(matchingIterator);

回调函数中

There should not be

IOObjectRelease(matchingIterator);

in the callback function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文