Skip to content

Instantly share code, notes, and snippets.

@edewit
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edewit/09b26eb57f3896a4fd92 to your computer and use it in GitHub Desktop.
Save edewit/09b26eb57f3896a4fd92 to your computer and use it in GitHub Desktop.
- (CDVPlugin*)initWithWebView:(UIWebView*)theWebView {
self = (GeofencingPlugin*)[super initWithWebView:(UIWebView*)theWebView];
if (self) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self; // Tells the location manager to send updates to this object
}
self.locationManager.distanceFilter = kCLLocationAccuracyBest;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
// Set a movement threshold for new events.
locationManager.distanceFilter = 100; // meters
[locationManager startUpdatingLocation];
return self;
}
- (void)startSignificantChangeUpdates
{
locationManager.delegate = self;
[locationManager startMonitoringSignificantLocationChanges];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
// If it's a relatively recent event, turn off updates to save power.
CLLocation* location = [locations lastObject];
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15.0) {
entered = NO;
}
}
- (void)notify {
if (entered) {
[self notify:region withStatus:@"entered"];
}
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
entered = YES;
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(notify:) userInfo:nil repeats:NO];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment