-
Notifications
You must be signed in to change notification settings - Fork 1
/
TIMAppLocator.m
61 lines (50 loc) · 2.15 KB
/
TIMAppLocator.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// TIMAppLocator.m
//
// Created by Tim Schröder on 23.03.13.
// Copyright (c) 2013 Tim Schröder. All rights reserved.
//
#import "TIMAppLocator.h"
#import "TIMAppLocatorProtocol.h"
@implementation TIMAppLocator
- (void)queryFinished:(NSNotification *)notification
{
NSMetadataQuery *aQuery = [notification object];
// Check query for results and extract paths
NSMutableArray *paths;
if (aQuery) {
NSInteger count = [aQuery resultCount];
if (count>0) {
paths = [NSMutableArray arrayWithCapacity:count];
int i;
for (i=0;i<count;i++) { // Fast enumeration not suitable here, see NSMetadataItem Documentation
NSMetadataItem *item = [aQuery resultAtIndex:i];
if (item) {
NSString *path = [item valueForAttribute:NSMetadataItemPathKey]; // vor 10.7: kMDItemPath
if (path) [paths addObject:path];
}
}
}
[aQuery stopQuery];
}
// Inform delegate
if (self.delegate && [self.delegate conformsToProtocol:@protocol(TIMAppLocatorProtocol)] && [self.delegate respondsToSelector:@selector(appLocations:)]) [self.delegate appLocations:paths];
}
-(BOOL)locateAppWithBundleIdentifier:(NSString*)bundleIdentifier
{
if (!bundleIdentifier) return NO;
if ([self.query isGathering]) return NO;
// Prepare query
self.query = [[NSMetadataQuery alloc] init]; // Allocate new instance every time, NSMetadataQuery is tricky
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(queryFinished:)
name:NSMetadataQueryDidFinishGatheringNotification
object:self.query];
[self.query setSearchScopes: [NSArray arrayWithObjects: NSMetadataQueryLocalComputerScope, nil]]; // Search everywhere
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"kMDItemCFBundleIdentifier = %@", bundleIdentifier];
[self.query setPredicate:predicate];
// Start Query
[self.query startQuery];
return YES;
}
@end