UTMLocationManager.m 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // Copyright © 2020 osy. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #import "UTMLocationManager.h"
  17. #import "UTMLogging.h"
  18. @import CoreLocation;
  19. @interface UTMLocationManager () <CLLocationManagerDelegate>
  20. @property (nonatomic) CLLocationManager *locationManager;
  21. @end
  22. @implementation UTMLocationManager
  23. + (instancetype)sharedInstance {
  24. static id sharedInstance = nil;
  25. static dispatch_once_t onceToken;
  26. dispatch_once(&onceToken, ^{
  27. sharedInstance = [[self alloc] init];
  28. UTMLocationManager *instance = sharedInstance;
  29. instance.locationManager = [[CLLocationManager alloc] init];
  30. instance.locationManager.delegate = instance;
  31. instance.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  32. instance.locationManager.pausesLocationUpdatesAutomatically = NO;
  33. });
  34. return sharedInstance;
  35. }
  36. - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
  37. }
  38. - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
  39. UTMLog(@"Location manager failed with: %@", error);
  40. }
  41. - (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager {
  42. if (manager.authorizationStatus == kCLAuthorizationStatusDenied) {
  43. UTMLog(@"Location services are disabled in settings.");
  44. } else {
  45. [self startUpdatingLocation];
  46. }
  47. }
  48. - (void)startUpdatingLocation {
  49. [self.locationManager requestAlwaysAuthorization];
  50. self.locationManager.allowsBackgroundLocationUpdates = YES;
  51. [self.locationManager startUpdatingLocation];
  52. }
  53. @end