У меня небольшая проблема с startMonitoringSignificantLocationChanges, похоже, она не работает. отображается значок местоположения, но моя функция делегата никогда не срабатывает
Я сделал класс для этого здесь:
заголовочный файл:
@interface geolocation : NSObject < CLLocationManagerDelegate > {
CLLocationManager *locationManager;
}
- (void) significantChange;
- (void) stopSignificantChange;
- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;
- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error;
@end
м файл:
@implementation geolocation
- (void) significantChange
{
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startMonitoringSignificantLocationChanges];
}
- (void) stopSignificantChange
{
[locationManager stopMonitoringSignificantLocationChanges];
}
- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UILocalNotification* alarm = [[UILocalNotification alloc] init];
if (alarm)
{
alarm.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
alarm.timeZone = [NSTimeZone defaultTimeZone];
alarm.repeatInterval = 0;
alarm.alertBody = @"Time to wake up!";
alarm.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:alarm];
}
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
});
}
- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
}
- (void) dealloc
{
locationManager.delegate = nil;
locationManager = nil;
}
@end
в AppDelegate я называю это:
- (void) applicationDidEnterBackground:(UIApplication *)application
{
[UIApplication sharedApplication].applicationIconBadgeNumber = 1;
[geo significantChange];
return [super applicationDidEnterBackground:application];
}