如何解决如何从目标C传递全局变量值以响应本机
我们已经创建了本机到本机的桥接反应,用于发送位置。
-
我们使用react RCT_EXPORT_METHOD进行回调。
-
我们创建一个对象c全局变量“ coordinate”以存储位置值并发送给回调方法
-
在调用RCT_EXPORT_METHOD进行回调时,我们的全局变量“ coordinate”在RCT_EXPORT_METHOD中显示为空
-
我们也将静态值附加在回调中,它们发送值以响应本机 RCT_EXPORT_METHOD(findEvents:(RCTResponseSenderBlock)回调) { callback(@ [@“ Test”]); }
-
如何在回调中传递全局变量值或通过其他任何方式发送我的位置数据以进行本地响应
我的.m文件代码:
#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
请访问帮助
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。