I'm trying to get the user position in iOS.
I guess the problem could be related to the dispatch_async.
I don't get errors during compilation but only the init function is called and the locationManager never gives an update.
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface LocationC: UIViewController <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
}
@end
@implementation LocationC
- (instancetype) init {
self = [super init];
if (self) {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[locationManager requestWhenInUseAuthorization];
[locationManager startMonitoringSignificantLocationChanges];
[locationManager startUpdatingLocation];
}
return self;
}
//never called
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"test")
}
@end
class LocationGet : public claid::Module
{
public:
void initialize()
{
dispatch_async(dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
//Background Thread
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
LocationC *example = [[LocationC alloc] init];
});
});
}
};
There might be a scenario in which the authorization status is not "Granted" when you call startMonitoring...
If the user sees the page for the first time and and location permission hasn't been authorized yet, requestWhenInUseAuthorization will display the dialogue, but you need to implement locationManager(_:didChangeAuthorization:) in order to know when the status has changed.
See this article: https://developer.apple.com/documentation/corelocation/cllocationmanager/1620562-requestwheninuseauthorization
It says: After the user makes a selection and determines the status, the location manager delivers the results to the delegate's locationManager(_:didChangeAuthorization:) method.
-- Update --
After another look, you should store a reference to the example instance, or else it would dealloc from memory before any location update is received. A UIViewController instance should be presented or pushed from another UIViewController instance, or alternatively it can be set as the root view controller of the application. Just instantiating it is not enough.