NSMetadataQuery not triggering in Sandbox on macOS 10.13

176 Views Asked by At

I have been using NSMetaDataQuery to know when user takes a screenshot in a sandbox on macOS prior to 10.13 and it was working fine. But on 10.13 i only get notified when i start NSMetaDataQuery, and on updates i am not receiving any notification. I even have used NSURL startAccessingSecurityScopedResource and stopAccessingSecurityScopedResource. Can not figure out what Apple has changed and how to make it work.

- (void)setScreenshotMonitoring {

    // Retrieve Scoped Bookmark url from defaults
    NSURL *url = [CoreUtils getScreenshotBookmarkUrl];
    [url startAccessingSecurityScopedResource];

    _query = [[NSMetadataQuery alloc] init];
    [_query setSearchScopes:@[url]];
    [_query setDelegate:self];
    [_query setPredicate:[NSPredicate predicateWithFormat:@"kMDItemIsScreenCapture = 1"]];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenshotQueryUpdated:) name:NSMetadataQueryDidStartGatheringNotification object:_query];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenshotQueryUpdated:) name:NSMetadataQueryDidUpdateNotification object:_query];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidFinish:) name:NSMetadataQueryDidFinishGatheringNotification object:_query];

    // Make sure Query starts on main thread
    runOnMainQueueWithoutDeadlocking(^{
        [_query startQuery];
    });
    [url stopAccessingSecurityScopedResource];
}

But screenshotQueryUpdated and queryDidFinish only called after startQuery not when new screenshots are taken.

0

There are 0 best solutions below