2011-07-04 2 views
2

J'ai le code affichant le gestionnaire de notificationObjectif C Échec du rappel

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(); 

Et ce gestionnaire de rappel

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) 
      { 
        ... 
      } 
    } 
} 

mais ma fonction de rappel est jamais appelée. J'ai essayé ceci avec le lecteur instantané d'USB sans succès. Où je me trompe?

Répondre

1

Il ne devrait pas être

IOObjectRelease(matchingIterator); 

dans la fonction de rappel.