6

I'm trying to use FSEvents in my sandboxed app to monitor some directories. I implemented the following class:

@implementation SNTracker

- (id)initWithPaths:(NSArray *)paths {
    self=[super init];
    if (self) {
        trackedPaths=paths;
        CFTimeInterval latency=1.0;
        FSEventStreamContext context={0,(__bridge void *)self,NULL,NULL,NULL};
        FSEventStreamRef eeventStream=FSEventStreamCreate(kCFAllocatorDefault,&callback,&context,(__bridge CFArrayRef)trackedPaths,kFSEventStreamEventIdSinceNow,latency,kFSEventStreamCreateFlagUseCFTypes|kFSEventStreamCreateFlagWatchRoot|kFSEventStreamCreateFlagFileEvents);
        FSEventStreamScheduleWithRunLoop(eeventStream,[[NSRunLoop mainRunLoop] getCFRunLoop],kCFRunLoopDefaultMode);
        FSEventStreamStart(eeventStream);
    }
    return self;
}

static void callback(ConstFSEventStreamRef streamRef,void *clientCallBackInfo,size_t numEvents,void *eventPaths,const FSEventStreamEventFlags eventFlags[],const FSEventStreamEventId eventIds[]) {
    NSLog(@"asd");
}

The problem is that "asd" never gets printed (i.e. the callback function is never called). When I disable "Enable App Sandboxing" in the Summary of my main target in Xcode, the callback gets called. Am I doing something wrong? The only entitlement I've given to the sandboxed app is read-write access to user selected files.

Nickkk
  • 2,261
  • 1
  • 25
  • 34
  • 1
    And the usere has selected the path you are trying to monitor via FSEvent? Since if he hasn't, you won't be allow to access it and thus also not monitor it. – Mecki Feb 19 '13 at 12:46
  • You're my salvation! I was calling `[myURL startAccessingSecurityScopedResource]` but also `[myURL stopAccessingSecurityScopedResource]`, so removing the last call solves my problem. Please add your comment as an answer, so I can tick it as solved :-) – Nickkk Feb 19 '13 at 13:35
  • @Nickkk: You probably should still call that eventually, just not right after you start monitoring. `dealloc` is probably a good place. – Peter Hosey Feb 19 '13 at 19:17

1 Answers1

3

And the usere has selected the path you are trying to monitor via FSEvent? Since if he hasn't, you won't be allow to access it and thus also not monitor it. A path can only be monitored as long as you are allowed to access it.

Mecki
  • 125,244
  • 33
  • 244
  • 253