flutter app untuk unitstock
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 

322 rindas
12 KiB

  1. #import "LocationPlugin.h"
  2. #ifdef COCOAPODS
  3. @import CoreLocation;
  4. #else
  5. #import <CoreLocation/CoreLocation.h>
  6. #endif
  7. @interface LocationPlugin() <FlutterStreamHandler, CLLocationManagerDelegate>
  8. @property (strong, nonatomic) CLLocationManager *clLocationManager;
  9. @property (copy, nonatomic) FlutterResult flutterResult;
  10. @property (assign, nonatomic) BOOL locationWanted;
  11. @property (assign, nonatomic) BOOL permissionWanted;
  12. @property (copy, nonatomic) FlutterEventSink flutterEventSink;
  13. @property (assign, nonatomic) BOOL flutterListening;
  14. @property (assign, nonatomic) BOOL hasInit;
  15. @end
  16. @implementation LocationPlugin
  17. +(void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  18. FlutterMethodChannel *channel =
  19. [FlutterMethodChannel methodChannelWithName:@"lyokone/location"
  20. binaryMessenger:registrar.messenger];
  21. FlutterEventChannel *stream =
  22. [FlutterEventChannel eventChannelWithName:@"lyokone/locationstream"
  23. binaryMessenger:registrar.messenger];
  24. LocationPlugin *instance = [[LocationPlugin alloc] init];
  25. [registrar addMethodCallDelegate:instance channel:channel];
  26. [stream setStreamHandler:instance];
  27. }
  28. -(instancetype)init {
  29. self = [super init];
  30. if (self) {
  31. self.locationWanted = NO;
  32. self.permissionWanted = NO;
  33. self.flutterListening = NO;
  34. self.hasInit = NO;
  35. }
  36. return self;
  37. }
  38. -(void)initLocation {
  39. if (!(self.hasInit)) {
  40. self.hasInit = YES;
  41. if ([CLLocationManager locationServicesEnabled]) {
  42. self.clLocationManager = [[CLLocationManager alloc] init];
  43. self.clLocationManager.delegate = self;
  44. self.clLocationManager.desiredAccuracy = kCLLocationAccuracyBest;
  45. }
  46. }
  47. }
  48. -(void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  49. [self initLocation];
  50. if ([call.method isEqualToString:@"changeSettings"]) {
  51. if ([CLLocationManager locationServicesEnabled]) {
  52. NSDictionary *dictionary = @{
  53. @"0" : @(kCLLocationAccuracyKilometer),
  54. @"1" : @(kCLLocationAccuracyHundredMeters),
  55. @"2" : @(kCLLocationAccuracyNearestTenMeters),
  56. @"3" : @(kCLLocationAccuracyBest),
  57. @"4" : @(kCLLocationAccuracyBestForNavigation)
  58. };
  59. self.clLocationManager.desiredAccuracy =
  60. [dictionary[call.arguments[@"accuracy"]] doubleValue];
  61. double distanceFilter = [call.arguments[@"distanceFilter"] doubleValue];
  62. if (distanceFilter == 0){
  63. distanceFilter = kCLDistanceFilterNone;
  64. }
  65. self.clLocationManager.distanceFilter = distanceFilter;
  66. result(@1);
  67. }
  68. } else if ([call.method isEqualToString:@"getLocation"]) {
  69. if (![CLLocationManager locationServicesEnabled]) {
  70. result([FlutterError errorWithCode:@"SERVICE_STATUS_DISABLED" message:@"Failed to get location. Location services disabled" details:nil]);
  71. return;
  72. }
  73. if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
  74. // Location services are requested but user has denied
  75. NSString *message = @"The user explicitly denied the use of location services for this "
  76. "app or location services are currently disabled in Settings.";
  77. result([FlutterError errorWithCode:@"PERMISSION_DENIED"
  78. message:message
  79. details:nil]);
  80. return;
  81. }
  82. self.flutterResult = result;
  83. self.locationWanted = YES;
  84. if ([self isPermissionGranted]) {
  85. [self.clLocationManager startUpdatingLocation];
  86. } else {
  87. [self requestPermission];
  88. if ([self isPermissionGranted]) {
  89. [self.clLocationManager startUpdatingLocation];
  90. }
  91. }
  92. } else if ([call.method isEqualToString:@"hasPermission"]) {
  93. if ([self isPermissionGranted]) {
  94. result(@1);
  95. } else {
  96. result(@0);
  97. }
  98. } else if ([call.method isEqualToString:@"requestPermission"]) {
  99. if ([self isPermissionGranted]) {
  100. result(@1);
  101. } else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
  102. self.flutterResult = result;
  103. self.permissionWanted = YES;
  104. [self requestPermission];
  105. } else {
  106. result(@2);
  107. }
  108. } else if ([call.method isEqualToString:@"serviceEnabled"]) {
  109. if ([CLLocationManager locationServicesEnabled]) {
  110. result(@1);
  111. } else {
  112. result(@0);
  113. }
  114. } else if ([call.method isEqualToString:@"requestService"]) {
  115. if ([CLLocationManager locationServicesEnabled]) {
  116. result(@1);
  117. } else {
  118. #if TARGET_OS_OSX
  119. NSAlert *alert = [[NSAlert alloc] init];
  120. [alert setMessageText:@"Location is Disabled"];
  121. [alert setInformativeText:@"To use location, go to your System Preferences > Security & Privacy > Privacy > Location Services."];
  122. [alert addButtonWithTitle:@"Open"];
  123. [alert addButtonWithTitle:@"Cancel"];
  124. [alert beginSheetModalForWindow:NSApplication.sharedApplication.mainWindow
  125. completionHandler:^(NSModalResponse returnCode) {
  126. if (returnCode == NSAlertFirstButtonReturn) {
  127. NSString *urlString = @"x-apple.systempreferences:com.apple.preference.security?Privacy_LocationServices";
  128. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:urlString]];
  129. } else {
  130. NSLog(@"Cancel");
  131. }
  132. }];
  133. #else
  134. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location is Disabled"
  135. message:@"To use location, go to your Settings App > Privacy > Location Services."
  136. delegate:self
  137. cancelButtonTitle:@"Cancel"
  138. otherButtonTitles:nil];
  139. [alert show];
  140. #endif
  141. result(@0);
  142. }
  143. } else {
  144. result(FlutterMethodNotImplemented);
  145. }
  146. }
  147. -(void) requestPermission {
  148. #if TARGET_OS_OSX
  149. if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"] != nil) {
  150. if (@available(macOS 10.15, *)) {
  151. [self.clLocationManager requestAlwaysAuthorization];
  152. }
  153. }
  154. #else
  155. if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"] != nil) {
  156. [self.clLocationManager requestWhenInUseAuthorization];
  157. }
  158. else if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"] != nil) {
  159. [self.clLocationManager requestAlwaysAuthorization];
  160. }
  161. #endif
  162. else {
  163. [NSException raise:NSInternalInconsistencyException format:
  164. @"To use location in iOS8 and above you need to define either "
  165. "NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription in the app "
  166. "bundle's Info.plist file"];
  167. }
  168. }
  169. -(BOOL) isPermissionGranted {
  170. BOOL isPermissionGranted = NO;
  171. CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
  172. #if TARGET_OS_OSX
  173. if (status == kCLAuthorizationStatusAuthorized) {
  174. // Location services are available
  175. isPermissionGranted = YES;
  176. } else if (@available(macOS 10.12, *)) {
  177. if (status == kCLAuthorizationStatusAuthorizedAlways) {
  178. // Location services are available
  179. isPermissionGranted = YES;
  180. }
  181. }
  182. #else //if TARGET_OS_IOS
  183. if (status == kCLAuthorizationStatusAuthorizedWhenInUse ||
  184. status == kCLAuthorizationStatusAuthorizedAlways) {
  185. // Location services are available
  186. isPermissionGranted = YES;
  187. }
  188. #endif
  189. else if (status == kCLAuthorizationStatusDenied ||
  190. status == kCLAuthorizationStatusRestricted) {
  191. // Location services are requested but user has denied / the app is restricted from
  192. // getting location
  193. isPermissionGranted = NO;
  194. } else if (status == kCLAuthorizationStatusNotDetermined) {
  195. // Location services never requested / the user still haven't decide
  196. isPermissionGranted = NO;
  197. } else {
  198. isPermissionGranted = NO;
  199. }
  200. return isPermissionGranted;
  201. }
  202. -(FlutterError*)onListenWithArguments:(id)arguments eventSink:(FlutterEventSink)events {
  203. self.flutterEventSink = events;
  204. self.flutterListening = YES;
  205. if ([self isPermissionGranted]) {
  206. [self.clLocationManager startUpdatingLocation];
  207. } else {
  208. [self requestPermission];
  209. }
  210. return nil;
  211. }
  212. -(FlutterError*)onCancelWithArguments:(id)arguments {
  213. self.flutterListening = NO;
  214. [self.clLocationManager stopUpdatingLocation];
  215. return nil;
  216. }
  217. #pragma mark - CLLocationManagerDelegate Methods
  218. -(void)locationManager:(CLLocationManager*)manager
  219. didUpdateLocations:(NSArray<CLLocation*>*)locations {
  220. CLLocation *location = locations.firstObject;
  221. NSTimeInterval timeInSeconds = [location.timestamp timeIntervalSince1970];
  222. NSDictionary<NSString*,NSNumber*>* coordinatesDict =
  223. @{
  224. @"latitude": @(location.coordinate.latitude),
  225. @"longitude": @(location.coordinate.longitude),
  226. @"accuracy": @(location.horizontalAccuracy),
  227. @"altitude": @(location.altitude),
  228. @"speed": @(location.speed),
  229. @"speed_accuracy": @0.0,
  230. @"heading": @(location.course),
  231. @"time": @(((double) timeInSeconds) * 1000.0) // in milliseconds since the epoch
  232. };
  233. if (self.locationWanted) {
  234. self.locationWanted = NO;
  235. self.flutterResult(coordinatesDict);
  236. }
  237. if (self.flutterListening) {
  238. self.flutterEventSink(coordinatesDict);
  239. } else {
  240. [self.clLocationManager stopUpdatingLocation];
  241. }
  242. }
  243. - (void)locationManager:(CLLocationManager *)manager
  244. didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
  245. if (status == kCLAuthorizationStatusDenied) {
  246. // The user denied authorization
  247. NSLog(@"User denied permissions");
  248. if (self.permissionWanted) {
  249. self.permissionWanted = NO;
  250. self.flutterResult(@0);
  251. }
  252. }
  253. #if TARGET_OS_OSX
  254. else if (status == kCLAuthorizationStatusAuthorized) {
  255. NSLog(@"User granted permissions");
  256. if (self.permissionWanted) {
  257. self.permissionWanted = NO;
  258. self.flutterResult(@1);
  259. }
  260. if (self.locationWanted || self.flutterListening) {
  261. [self.clLocationManager startUpdatingLocation];
  262. }
  263. } else if (@available(macOS 10.12, *)) {
  264. if (status == kCLAuthorizationStatusAuthorizedAlways) {
  265. NSLog(@"User granted permissions");
  266. if (self.permissionWanted) {
  267. self.permissionWanted = NO;
  268. self.flutterResult(@1);
  269. }
  270. if (self.locationWanted || self.flutterListening) {
  271. [self.clLocationManager startUpdatingLocation];
  272. }
  273. }
  274. }
  275. #else //if TARGET_OS_IOS
  276. else if (status == kCLAuthorizationStatusAuthorizedWhenInUse ||
  277. status == kCLAuthorizationStatusAuthorizedAlways) {
  278. NSLog(@"User granted permissions");
  279. if (self.permissionWanted) {
  280. self.permissionWanted = NO;
  281. self.flutterResult(@1);
  282. }
  283. if (self.locationWanted || self.flutterListening) {
  284. [self.clLocationManager startUpdatingLocation];
  285. }
  286. }
  287. #endif
  288. }
  289. @end