Skip to content

Instantly share code, notes, and snippets.

@HalCanary
Last active July 6, 2024 13:41
Show Gist options
  • Save HalCanary/3979aac6e75f4e0cdaf5d53a6581063a to your computer and use it in GitHub Desktop.
Save HalCanary/3979aac6e75f4e0cdaf5d53a6581063a to your computer and use it in GitHub Desktop.
Location/Location.m
// Exmaple of using CoreLocation on macOS.
//
// Link with:
// -framework AppKit
// -framework CoreLocation
#include <stdio.h>
#include <stdlib.h>
#include <AppKit/AppKit.h>
#include <CoreLocation/CoreLocation.h>
@interface Delegate : NSObject <CLLocationManagerDelegate>
@property (strong) CLLocationManager* mgr;
@end
@implementation Delegate
- (instancetype) init {
[self setMgr:[[CLLocationManager alloc] init]];
[self.mgr setDelegate:self];
[self.mgr setDesiredAccuracy:kCLLocationAccuracyBest];
[self.mgr setDistanceFilter:2.0];
[self.mgr requestAlwaysAuthorization];
[self.mgr startUpdatingLocation];
return self;
}
- (void)locationManager:(CLLocationManager*)m didFailWithError:(NSError*)e {
fprintf(stderr, "CLLocationManager error: %s\n",
[[e localizedDescription] UTF8String]);
exit(1);
}
- (void)locationManager:(CLLocationManager*)m
didUpdateToLocation:(CLLocation*)l
fromLocation:(CLLocation*)ol {
printf("%f, %f", l.coordinate.latitude, l.coordinate.longitude);
[NSApp terminate:self];
}
@end
int main(int argc, const char* argv[]) {
Delegate* d = [[Delegate alloc] init];
[[NSApplication sharedApplication] setActivationPolicy:NSApplicationActivationPolicyProhibited];
NSApplicationMain(argc, argv);
[d release];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment