它是第一次添加或删除设备时工作,但之后我的回调永远不会被调用.我弄不清楚为什么?谁能发现我哪里错了?
(xcode项目,如果你想测试)@H_419_12@http://monkeyfood.com/testIOKitNOtificaiton.zip
谢谢.
//// AppDelegate.m// testIOKitNotification//// Created by Diggory Laycock on 23/07/2012.// copyright (c) 2012 MonkeyFood.com. All rights reserved.//#import "AppDelegate.h"@implementation AppDelegate// Arduino USB info#define matchvendorID 0x2341 #define matchProductID 0x0043#pragma mark -#pragma mark C Callback functions#pragma mark -voID usbDeviceAppeared(voID *refCon,io_iterator_t iterator){ NSLog(@"Matching USB device appeared");}voID usbDevicedisappeared(voID *refCon,io_iterator_t iterator){ NSLog(@"Matching USB device disappeared");}@synthesize window = _window;#pragma mark -#pragma mark Application Methods#pragma mark -- (voID)applicationDIDFinishLaunching:(NSNotification *)aNotification{ io_iterator_t newDevicesIterator; io_iterator_t lostDevicesIterator; newDevicesIterator = 0; lostDevicesIterator = 0; NSLog(@" "); NSMutableDictionary *matchingDict = (__brIDge NSMutableDictionary *)IOServiceMatching(kIoUSBDeviceClassname); if (matchingDict == nil){ NSLog(@"Could not create matching dictionary"); return; } [matchingDict setobject:[NSNumber numberWithShort:matchvendorID] forKey:(Nsstring *)CFSTR(kUSBvendorID)]; [matchingDict setobject:[NSNumber numberWithShort:matchProductID] forKey:(Nsstring *)CFSTR(kUSBProductID)]; // Add notification ports to runloop IONotificationPortRef notificationPort = IONotificationPortCreate(kIOMasterPortDefault); CFRunLoopSourceRef notificationRunLoopSource = IONotificationPortGetRunLoopSource(notificationPort); CFRunLoopAddSource([[NSRunLoop currentRunLoop] getCFRunLoop],notificationRunLoopSource,kcfRunLoopDefaultMode); kern_return_t err; err = IOServiceAddMatchingNotification(notificationPort,kIOMatchednotification,(__brIDge CFDictionaryRef)matchingDict,usbDeviceAppeared,(__brIDge voID *)self,&newDevicesIterator); if (err) { NSLog(@"error adding publish notification"); } [self matchingDevicesAdded: newDevicesIterator]; NSMutableDictionary *matchingDictRemoved = (__brIDge NSMutableDictionary *)IOServiceMatching(kIoUSBDeviceClassname); if (matchingDictRemoved == nil){ NSLog(@"Could not create matching dictionary"); return; } [matchingDictRemoved setobject:[NSNumber numberWithShort:matchvendorID] forKey:(Nsstring *)CFSTR(kUSBvendorID)]; [matchingDictRemoved setobject:[NSNumber numberWithShort:matchProductID] forKey:(Nsstring *)CFSTR(kUSBProductID)]; err = IOServiceAddMatchingNotification(notificationPort,kIOTerminatednotification,(__brIDge CFDictionaryRef)matchingDictRemoved,usbDevicedisappeared,&lostDevicesIterator); if (err) { NSLog(@"error adding removed notification"); } [self matchingDevicesRemoved: lostDevicesIterator]; // CFRunLoopRun(); // [[NSRunLoop currentRunLoop] run];}#pragma mark -#pragma mark ObjC Callback functions#pragma mark -- (voID)matchingDevicesAdded:(io_iterator_t)devices{ io_object_t thisObject; while ( (thisObject = IOIteratorNext(devices))) { NSLog(@"new Matching device added "); IOObjectRelease(thisObject); } }- (voID)matchingDevicesRemoved:(io_iterator_t)devices{ io_object_t thisObject; while ( (thisObject = IOIteratorNext(devices))) { NSLog(@"A matching device was removed "); IOObjectRelease(thisObject); } }@end解决方法 我弄清楚了什么是错的 – 我没有对C Callback中的迭代器做任何事情.一个愚蠢的错误! 总结
以上是内存溢出为你收集整理的macos – IOKit设备添加/删除通知 – 只触发一次?全部内容,希望文章能够帮你解决macos – IOKit设备添加/删除通知 – 只触发一次?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)