ReactNativePermissions.m 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // ReactNativePermissions.m
  3. // ReactNativePermissions
  4. //
  5. // Created by Yonah Forst on 18/02/16.
  6. // Copyright © 2016 Yonah Forst. All rights reserved.
  7. //
  8. #import "ReactNativePermissions.h"
  9. #import "RCTBridge.h"
  10. #import "RCTConvert.h"
  11. #import "RCTEventDispatcher.h"
  12. @interface ReactNativePermissions()
  13. @end
  14. @implementation ReactNativePermissions
  15. RCT_EXPORT_MODULE();
  16. @synthesize bridge = _bridge;
  17. #pragma mark Initialization
  18. - (instancetype)init
  19. {
  20. if (self = [super init]) {
  21. }
  22. return self;
  23. }
  24. RCT_REMAP_METHOD(start, start:(int)headingFilter resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
  25. // Start heading updates.
  26. if ([CLLocationManager headingAvailable]) {
  27. if (!headingFilter)
  28. headingFilter = 5;
  29. self.locManager.headingFilter = headingFilter;
  30. [self.locManager startUpdatingHeading];
  31. resolve(@YES);
  32. } else {
  33. resolve(@NO);
  34. }
  35. }
  36. RCT_EXPORT_METHOD(stop) {
  37. [self.locManager stopUpdatingHeading];
  38. }
  39. - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
  40. if (newHeading.headingAccuracy < 0)
  41. return;
  42. // Use the true heading if it is valid.
  43. CLLocationDirection heading = ((newHeading.trueHeading > 0) ?
  44. newHeading.trueHeading : newHeading.magneticHeading);
  45. NSDictionary *headingEvent = @{@"heading": @(heading)};
  46. [self.bridge.eventDispatcher sendDeviceEventWithName:@"headingUpdated" body:headingEvent];
  47. }
  48. @end