How to pass global variable value from objective c to react native

Viewed 308

we have create a bridge react native to native for send location.

  1. we use react RCT_EXPORT_METHOD for callback.

  2. we create a object c global variable "coordinate" to store our location value and send to callback method

  3. when call RCT_EXPORT_METHOD for callback our global variable "coordinate" show null in RCT_EXPORT_METHOD

  4. Also we append static value in callback they send value to react native RCT_EXPORT_METHOD(findEvents:(RCTResponseSenderBlock)callback ) { callback(@[@"Test"]); }

  5. how to pass global variable value in callback or any other way to send my location data to react native

My .m file code:

#import "AppDelegate.h"
#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <Firebase.h>

@implementation AppDelegate{
  RCTRootView *rootView;
  NSMutableDictionary *coordinate;
}

RCT_EXPORT_MODULE();
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary 
*)launchOptions
{
 RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
 rootView = [[RCTRootView alloc] initWithBridge:bridge
                                               moduleName:@"OHCSMobile1"
                                        initialProperties:nil];

 rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];

if ([FIRApp defaultApp] == nil) {
  [FIRApp configure];
}

 self.manager = [[APScheduledLocationManager alloc] initWithDelegate:self];

 return YES;
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
 #if DEBUG
  return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" 
fallbackResource:nil];
#else
return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}

-(void)scheduledLocationManager:(APScheduledLocationManager *)manager didFailWithError: 
(NSError *)error{
 NSLog(@"error");
}
-(void)scheduledLocationManager:(APScheduledLocationManager *)manager didUpdateLocations: 
(NSArray<CLLocation *> *)locations{
NSLog(@"locations");
CLLocation * location = locations[0];
 
 double latitute = location.coordinate.latitude;
 double longitute = location.coordinate.longitude;


coordinate = [[NSMutableDictionary alloc] init];
[coordinate setValue:[NSNumber numberWithDouble: latitute] forKey:@"LAT"];
[coordinate setValue:[NSNumber numberWithDouble: longitute] forKey:@"LONG"];



NSLog(@"%@oordinate", coordinate);

}
 -(void)scheduledLocationManager:(APScheduledLocationManager *)manager 
didChangeAuthorization: 
(CLAuthorizationStatus)status{
 NSLog(@"Authorization");
 if (status == kCLAuthorizationStatusAuthorizedAlways || status == 
 kCLAuthorizationStatusAuthorizedWhenInUse) {
     // The user accepted authorization
    [self.manager startUpdatingLocationWithInterval:30 acceptableLocationAccuracy:100];
 }
}

RCT_EXPORT_METHOD(findEvents:(RCTResponseSenderBlock)callback )
 {
 if(coordinate != nil){
callback(@[coordinate]);
}else{
callback(@[@"null"]);
}

 }
 @end

PLease Help

0 Answers
Related