-
Notifications
You must be signed in to change notification settings - Fork 1
/
LocationControlla.m
106 lines (80 loc) · 3.4 KB
/
LocationControlla.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
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
//
// LocationController.m
// location-reminders
//
// Created by Elyanil Liranzo Castro on 5/2/17.
// Copyright © 2017 Elyanil Liranzo Castro. All rights reserved.
//
#import "LocationControlla.h"
@import UserNotifications;
@interface LocationControlla () <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLLocation *location;
@end
@implementation LocationControlla
+(instancetype)shared{
static LocationControlla *shared = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shared = [[[self class] alloc] init];
});
return shared;
}
-(instancetype)init{
self = [super init];
if (self) {
[self requestPermissions];
self.location = [[CLLocation alloc] init];
}
return self;
}
-(void)requestPermissions {
self.locationManager = [[CLLocationManager alloc]init];
[[self locationManager] requestAlwaysAuthorization];
[[self locationManager]setDelegate:self];
[[self locationManager] setDesiredAccuracy:kCLLocationAccuracyBest];
[[self locationManager] setDistanceFilter: 100];
[[self locationManager] startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
CLLocation *location = [locations lastObject];
[self.delegate locationControllaUpdatedLocation:location];
}
-(void)locationManager:(CLLocationManager *)manager //ignore if in simulator
didFailWithError:(NSError *)error{
if (error) {
NSLog(@"Failed to find location : %@", error.localizedDescription);
}
}
-(void)locationControllerUpdatedLocation:(CLLocation *)location{
NSLog(@"Here is that location: Lat: %f Long: %f",location.coordinate.latitude, location.coordinate.longitude);
}
-(void)startMonitoringForRegion:(CLRegion *)region{
[[self locationManager] startMonitoringForRegion:region];
}
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
[content setTitle:@"Reminder"];
[content setBody:[NSString stringWithFormat:@"%@", region.identifier]];
[content setSound:[UNNotificationSound defaultSound]];
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.1 repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Location Entered" content:content trigger:trigger];
UNUserNotificationCenter *current = [UNUserNotificationCenter currentNotificationCenter];
[current removeAllPendingNotificationRequests];
[current addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"Failed to send notification: Error: %@",error.localizedDescription);
}
}];
NSLog(@"User did ENTER Region: %@", region.identifier);
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region{
NSLog(@"User did EXIT region: %@",region.identifier);
}
-(void)locationManager:(CLLocationManager *)manager didVisit:(CLVisit *)visit{
NSLog(@"didVisit: %@",visit);
}
-(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region{
NSLog(@"didStartMonitorForRegion: %@",region.identifier);
}
@end