You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native权限在iOS模拟器中始终返回unavailable问题求助

问题:iOS模拟器中位置权限状态返回unavailable导致弹窗异常显示

我开发的React Native应用使用react-native-permissions处理位置权限,逻辑是当用户未选择“始终允许”时显示自定义弹窗。该功能在Android上运行正常,但iOS模拟器中即便已选择“始终允许”,弹窗仍持续显示。

相关代码

useEffect(() => {
  checkLocationPermission();
}, []);

const checkLocationPermission = async () => {
  const permission =
    Platform.OS === 'android'
      ? PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION
      : PERMISSIONS.IOS.LOCATION_ALWAYS;

  const permissionStatus = await check(permission);

  if (
    permissionStatus !== RESULTS.GRANTED &&
    permissionStatus !== RESULTS.BLOCKED
  ) {
    setCustomModal(true);
  }
};

通过console.log发现,iOS端permissionStatus返回值为unavailable。

已配置内容

  • Info.plist:
<key>NSLocationAlwaysUsageDescription</key>
<string></string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
  • Podfile:
permissions_path = '../node_modules/react-native-permissions/ios'
pod 'Permission-LocationWhenInUse', :path => "#{permissions_path}/LocationWhenInUse"

解决方案

  1. 补全Pod依赖
    要请求LOCATION_ALWAYS权限,必须同时引入Permission-LocationAlways依赖,修改Podfile:
permissions_path = '../node_modules/react-native-permissions/ios'
pod 'Permission-LocationWhenInUse', :path => "#{permissions_path}/LocationWhenInUse"
pod 'Permission-LocationAlways', :path => "#{permissions_path}/LocationAlways"

修改后执行pod install更新依赖。

  1. 修正Info.plist配置
    iOS 11+已废弃NSLocationAlwaysUsageDescription,需替换为NSLocationAlwaysAndWhenInUseUsageDescription,且权限描述不能为空字符串:
<key>NSLocationWhenInUseUsageDescription</key>
<string>需要使用您的位置信息以提供基础服务</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>需要始终使用您的位置信息以在后台提供服务</string>
  1. 调整iOS权限检查逻辑
    iOS系统要求,请求始终允许权限前必须先获取使用期间允许权限,调整检查函数:
const checkLocationPermission = async () => {
  let permissionStatus;
  if (Platform.OS === 'android') {
    const permission = PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION;
    permissionStatus = await check(permission);
  } else {
    // 先检查「使用期间允许」权限
    const whenInUseStatus = await check(PERMISSIONS.IOS.LOCATION_WHEN_IN_USE);
    if (whenInUseStatus === RESULTS.GRANTED) {
      // 已授权基础权限,再检查「始终允许」
      permissionStatus = await check(PERMISSIONS.IOS.LOCATION_ALWAYS);
    } else {
      permissionStatus = whenInUseStatus;
    }
  }

  if (permissionStatus !== RESULTS.GRANTED && permissionStatus !== RESULTS.BLOCKED) {
    setCustomModal(true);
  }
};
  1. 重置模拟器缓存
    iOS模拟器的权限状态可能存在缓存,执行Device > Erase All Content and Settings重置模拟器,重新运行应用测试。

内容的提问来源于stack exchange,提问作者Bryl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 07:55:12