react native 开启地理位置受权

作者 : 开心源码 本文共2466个字,预计阅读时间需要7分钟 发布时间: 2022-05-12 共156人阅读

在用RN 做获取经纬度的时候,发现地理位置在info中配置了,但是还是没有提醒客户受权,
而后就在原生加了主动受权
原生部分代码:
在 AppDelegate.m中 :
首选 CLLocationManager *locationManager;
实例话要改成全局的,不然的话会出现 受权弹出框一闪而过的现象
info配置项

<key>NSLocationAlwaysUsageDescription</key>    <string>始终访问地理位置</string>    <key>NSLocationWhenInUseUsageDescription</key>    <string>在使用期间访问地理位置</string>    <key>NSPhotoLibraryUsageDescription</key>

方法:

- (void) getUserLocation {  int status=[CLLocationManager authorizationStatus];  if (![CLLocationManager locationServicesEnabled] || status < 3) {    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8)    {      locationManager = [[CLLocationManager alloc] init];      [locationManager requestAlwaysAuthorization];      [locationManager requestWhenInUseAuthorization];    }  }}

初始化的时候调用

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];  [RNSplashScreen show];  //显示启动屏  [self getUserLocation];  return YES;}

主动触发 相关交互类 .m文件代码

#import "openGps.h"#import <CoreLocation/CoreLocation.h>#import <WebKit/WebKit.h>@implementation openGpsCLLocationManager  *locationManager;RCT_EXPORT_MODULE(openGps)RCT_EXPORT_METHOD(openGPS){    BOOL enable=[CLLocationManager locationServicesEnabled];  NSInteger status=[CLLocationManager authorizationStatus];  if(!enable || status<3)  {    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8)    {      locationManager = [[CLLocationManager alloc] init];      [locationManager requestAlwaysAuthorization];      [locationManager requestWhenInUseAuthorization];    }    UIAlertController *alertView=[UIAlertController alertControllerWithTitle:@"APP想要访问您的地理位置以方便订单审核" message:@"定位服务未开启,请进入系统[设置]> [隐私] > [定位服务]中打开开关,并允许使用定位服务" preferredStyle:UIAlertControllerStyleAlert];    UIAlertAction *sureAction=[UIAlertAction actionWithTitle:@"立即开启" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];    }];    UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {    }];    [alertView addAction:sureAction];    [alertView addAction:cancelAction];    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];    UIViewController *mainViewController = keyWindow.rootViewController;    [mainViewController presentViewController:alertView animated:YES completion:nil];  }  }@end

rn 端调用:

   NativeModules.openGps.openGPS()

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » react native 开启地理位置受权

发表回复