美文网首页React Native学习程序员
react native 开启地理位置授权

react native 开启地理位置授权

作者: JsLin_ | 来源:发表于2018-09-17 20:06 被阅读14次

    效果图:


    image.png

    在用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 openGps
    CLLocationManager  *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()
    

    相关文章

      网友评论

        本文标题:react native 开启地理位置授权

        本文链接:https://www.haomeiwen.com/subject/qhddnftx.html