CParam.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // CParam.m
  3. // CParam
  4. //
  5. // Created by breeze on 2017/6/20.
  6. // Copyright © 2017年 Breeze. All rights reserved.
  7. //
  8. #import "CParam.h"
  9. #import <React/RCTBridge.h>
  10. #import <React/RCTEventDispatcher.h>
  11. #import "DataController.h"
  12. #import "LocationManager.h"
  13. #import <Foundation/NSTimer.h>
  14. static CParam *_instance;
  15. @interface CParam () <LocationManagerDelegate>
  16. @property (nonatomic) bool hasListeners;
  17. @property (nonatomic, strong) NSString *eventName;
  18. @property (nonatomic, strong) NSTimer *timer;
  19. @property (nonatomic, strong) NSDictionary *cparams;
  20. @end
  21. @implementation CParam
  22. RCT_EXPORT_MODULE();
  23. +(instancetype)allocWithZone:(struct _NSZone *)zone
  24. {
  25. static dispatch_once_t onceToken;
  26. dispatch_once(&onceToken, ^{
  27. if (_instance == nil) {
  28. _instance = [super allocWithZone:zone];
  29. }
  30. });
  31. return _instance;
  32. }
  33. +(instancetype)shareInstance {
  34. return [[self alloc] init];
  35. }
  36. // 为了严谨,也要重写copyWithZone 和 mutableCopyWithZone
  37. -(id)copyWithZone:(NSZone *)zone {
  38. return _instance;
  39. }
  40. -(id)mutableCopyWithZone:(NSZone *)zone {
  41. return _instance;
  42. }
  43. - (void)dealloc {
  44. if (_timer.valid) {
  45. [_timer invalidate];
  46. }
  47. }
  48. + (BOOL)requiresMainQueueSetup {
  49. return YES;
  50. }
  51. - (instancetype)init {
  52. if (self = [super init]) {
  53. // [LocationManager shareInstance].delegate = self;
  54. //
  55. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  56. // [[LocationManager shareInstance] startUpdatingLocation];
  57. // });
  58. //
  59. // self.timer = [NSTimer scheduledTimerWithTimeInterval: 300
  60. // target: self
  61. // selector: @selector(timeout)
  62. // userInfo: nil
  63. // repeats: YES];
  64. }
  65. return self;
  66. }
  67. - (void)timeout {
  68. [[LocationManager shareInstance] startUpdatingLocation];
  69. }
  70. - (NSString *)eventName {
  71. return @"GPS_UPDATE";
  72. }
  73. - (NSArray<NSString *> *)supportedEvents {
  74. return @[self.eventName];
  75. }
  76. - (NSDictionary<NSString *, id> *)constantsToExport {
  77. return @{@"GPSUpdateEventName": self.eventName};
  78. }
  79. // 在添加第一个监听函数时触发
  80. -(void)startObserving {
  81. self.hasListeners = YES;
  82. // Set up any upstream listeners or background tasks as necessary
  83. }
  84. // Will be called when this module's last listener is removed, or on dealloc.
  85. -(void)stopObserving {
  86. self.hasListeners = NO;
  87. // Remove upstream listeners, stop unnecessary background tasks
  88. }
  89. - (void)updateLocationWithCoordinate:(CLLocationCoordinate2D)coordinate {
  90. if (self.hasListeners) { // Only send events if anyone is listening
  91. NSNumber *latitude = [NSNumber numberWithFloat:coordinate.latitude];
  92. NSNumber *longitude = [NSNumber numberWithFloat:coordinate.longitude];
  93. [self sendEventWithName: self.eventName body:@{@"lat": latitude, @"lng": longitude}];
  94. }
  95. }
  96. RCT_EXPORT_METHOD(setNativeCparams: (nonnull NSDictionary *)cparams) {
  97. NSMutableDictionary *resDict = [[NSMutableDictionary alloc] init];
  98. for (NSString *key in cparams) {
  99. id val = cparams[key];
  100. if (val) {
  101. [resDict setValue: val forKey: key];
  102. }
  103. }
  104. _cparams = [resDict copy];
  105. }
  106. - (NSDictionary *)cparams {
  107. return _cparams;
  108. }
  109. @end