summaryrefslogtreecommitdiff
path: root/platform/ios/MGLMetricsLocationManager.m
blob: 5eac0370dc359c563c5ecf8527f0af1ea9512b80 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//
//  MBLocationManager.m
//  Hermes
//
//  Dynamic Settings.bundle loading based on:
//  http://stackoverflow.com/questions/510216/can-you-make-the-settings-in-settings-bundle-default-even-if-you-dont-open-the
//
//  Created by Brad Leege on 3/8/15.
//  Copyright (c) 2015 Mapbox. All rights reserved.
//

#import "MGLMetricsLocationManager.h"
#import "CoreLocation/CoreLocation.h"
#import "MGLMapboxEvents.h"

@interface MGLMetricsLocationManager()

@property (atomic) CLLocationManager *locationManager;

@end

@implementation MGLMetricsLocationManager

static MGLMetricsLocationManager *sharedManager = nil;

- (id) init {
    if (self = [super init]) {
        
        NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
        if(!settingsBundle) {
            NSLog(@"Could not find Settings.bundle");
        } else {
            NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"]];
            NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"];
            NSMutableDictionary *defaultsToRegister = [[NSMutableDictionary alloc] initWithCapacity:[preferences count]];
            for(NSDictionary *prefSpecification in preferences) {
                NSString *key = [prefSpecification objectForKey:@"Key"];
                if(key && [[prefSpecification allKeys] containsObject:@"DefaultValue"]) {
                    [defaultsToRegister setObject:[prefSpecification objectForKey:@"DefaultValue"] forKey:key];
                }
            }
            
            [[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister];
        }
        
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.distanceFilter = 2;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        _locationManager.pausesLocationUpdatesAutomatically = YES;
        [_locationManager setDelegate:self];
    }
    return self;
}

+ (id)sharedManager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [[self alloc] init];
    });
    return sharedManager;
}

- (BOOL) isAuthorizedStatusDetermined {
    return ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusNotDetermined);
}

- (void) requestAlwaysAuthorization {
    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [self.locationManager requestAlwaysAuthorization];
    } else {
        // This is iOS 7 or below so Starting Location Updates will trigger authorization request
        [self startUpdatingLocation];
    }
}

- (void) startUpdatingLocation {
    [self.locationManager startUpdatingLocation];
}

- (void) stopUpdatingLocation {
    [self.locationManager stopUpdatingLocation];
}

#pragma mark CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation *loc = (CLLocation *)[locations objectAtIndex:0];
    NSLog(@"didUpdateLocations() called  with %lu location in array.  First Location = %f, %f", (unsigned long)locations.count, loc.coordinate.latitude, loc.coordinate.longitude);
    
    //  Iterate through locations to pass all data
    for (CLLocation *loc in locations) {
        NSMutableDictionary *evt = [[NSMutableDictionary alloc] init];
        [evt setValue:[[NSNumber alloc] initWithDouble:loc.coordinate.latitude] forKey:@"lat"];
        [evt setValue:[[NSNumber alloc] initWithDouble:loc.coordinate.longitude] forKey:@"lng"];
        [[MGLMapboxEvents sharedManager] pushEvent:@"location" withAttributes:evt];
    }
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    NSString *newStatus = nil;
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
            newStatus = @"User Hasn't Determined Yet";
            break;
        case kCLAuthorizationStatusRestricted:
            newStatus = @"Restricted and Can't Be Changed By User";
            break;
        case kCLAuthorizationStatusDenied:
            newStatus = @"User Explcitly Denied";
            [[MGLMetricsLocationManager sharedManager] stopUpdatingLocation];
            break;
        case kCLAuthorizationStatusAuthorized:
            newStatus = @"User Has Authorized / Authorized Always";
            [[MGLMetricsLocationManager sharedManager] startUpdatingLocation];
            break;
            //        case kCLAuthorizationStatusAuthorizedAlways:
            //            newStatus = @"Not Determined";
            //            break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:
            newStatus = @"User Has Authorized When In Use Only";
            [[MGLMetricsLocationManager sharedManager] startUpdatingLocation];
            break;
        default:
            newStatus = @"Unknown";
            break;
    }
    NSLog(@"MGLMetricsLocationManager didChangeAuthorizationStatus() called.  New Status = %@", newStatus);
}

@end