CCManageLocation.m 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // CCManageLocation.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 23/07/15.
  6. // Copyright (c) 2015 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "CCManageLocation.h"
  24. #import "AppDelegate.h"
  25. #import "NCBridgeSwift.h"
  26. @implementation CCManageLocation
  27. + (CCManageLocation *)sharedInstance
  28. {
  29. static CCManageLocation *sharedInstance;
  30. @synchronized(self)
  31. {
  32. if (!sharedInstance){
  33. sharedInstance = [CCManageLocation new];
  34. }
  35. return sharedInstance;
  36. }
  37. }
  38. - (void)startSignificantChangeUpdates
  39. {
  40. if (self.locationManager == nil) {
  41. self.locationManager = [[CLLocationManager alloc] init];
  42. self.locationManager.delegate = self;
  43. if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
  44. [self.locationManager requestAlwaysAuthorization];
  45. }
  46. [self.locationManager startMonitoringSignificantLocationChanges];
  47. }
  48. - (void)stopSignificantChangeUpdates
  49. {
  50. [self.locationManager stopMonitoringSignificantLocationChanges];
  51. }
  52. - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
  53. {
  54. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  55. CLLocation* location = [locations lastObject];
  56. [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"update location manager: latitude %+.6f, longitude %+.6f", location.coordinate.latitude, location.coordinate.longitude]];
  57. appDelegate.currentLatitude = location.coordinate.latitude;
  58. appDelegate.currentLongitude = location.coordinate.longitude;
  59. [self.delegate changedLocation];
  60. }
  61. - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
  62. {
  63. [self.delegate statusAuthorizationLocationChanged];
  64. }
  65. - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
  66. {
  67. [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Location manager didFailWithError: Unable to start location manager, %@", [error description]]];
  68. [self.delegate statusAuthorizationLocationChanged];
  69. }
  70. @end