#import <CoreLocation/CoreLocation.h> typedef void(^locationHandler)(CLLocation *location); @interface DLLocationTracker : NSObject <CLLocationManagerDelegate> @property (nonatomic, strong) CLLocationManager *locationManager; @property (nonatomic, copy) locationHandler locationUpdatedInForeground; - (void)startUpdatingLocation; - (void)stopUpdatingLocation; @end
#import <CoreLocation/CoreLocation.h> typedef void(^locationHandler)(CLLocation *location); @interface DLLocationTracker : NSObject <CLLocationManagerDelegate> @property (nonatomic, strong) CLLocationManager *locationManager; @property (nonatomic, copy) locationHandler locationUpdatedInForeground; @property (nonatomic, copy) locationHandler locationUpdatedInBackground; - (void)startUpdatingLocation; - (void)stopUpdatingLocation; - (void)endBackgroundTask; @end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) { self.locationTracker = [[DLLocationTracker alloc] init]; [self.locationTracker setLocationUpdatedInBackground:^(CLLocation *location) { // , local notification UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:15]; notification.alertBody = [NSString stringWithFormat:@"New location: %@", location]; [[UIApplication sharedApplication] scheduleLocalNotification:notification]; }]; [self.locationTracker startUpdatingLocation]; } ..... }
- (id)init { if (self = [super init]) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground) name: UIApplicationDidEnterBackgroundNotification object:nil]; self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; } return self; }
- (void)startUpdatingLocation { [self stopUpdatingLocation]; [self isInBackground] ? [self.locationManager startMonitoringSignificantLocationChanges] : [self.locationManager startUpdatingLocation]; }
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { // if (oldLocation && ([newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp] < kMinUpdateTime || [newLocation distanceFromLocation:oldLocation] < kMinUpdateDistance)) { return; } if ([self isInBackground]) { if (self.locationUpdatedInBackground) { bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler: ^{ [[UIApplication sharedApplication] endBackgroundTask:bgTask]; }]; self.locationUpdatedInBackground(newLocation); [self endBackgroundTask]; } } else { // - if (self.locationUpdatedInForeground) { self.locationUpdatedInForeground(newLocation); } } }
- (void)endBackgroundTask { if (bgTask != UIBackgroundTaskInvalid) { [[UIApplication sharedApplication] endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; } }
if ([self isInBackground]) { if (self.locationUpdatedInBackground) { bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler: ^{ [[UIApplication sharedApplication] endBackgroundTask:bgTask]; }]; self.locationUpdatedInBackground(newLocation); //[self endBackgroundTask]; - }
__weak DLLocationTracker *lc = self.locationTracker; [self.locationTracker setLocationUpdatedInBackground:^ (CLLocation *location) { //, completion fail [self sendLocationToServer:location completion:^{ [lc endBackGroundTask]; } fail:^(NSError *fail) { [lc endBackGroundTask]; }]; }];
Source: https://habr.com/ru/post/149836/
All Articles