React Native 64.0.3使用react-native-wifi-reborn无法检测SSID求助
解决React Native 64.0.3使用react-native-wifi-reborn获取WiFi SSID失败的问题
先修复Info.plist的配置错误
你的Info.plist存在多处拼写和语法错误,这是权限申请失败的核心原因之一,修正后的配置如下:
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key> <string>The App uses the location for the network SSID</string> <key>NSLocationAlwaysUsageDescription</key> <string>The App uses the location for the network SSID</string> <key>NSLocationTemporaryUsageDescriptionDictionary</key> <dict> <key>SSID-Purpose</key> <string>The App uses the location for the network SSID</string> </dict> <key>NSLocationWhenInUseUsageDescription</key> <string>The App uses the location for the network SSID</string>
错误说明:
- 拼写错误:
NSLocationAlwaysAndwhenInUseUsageDescription中的when需大写为When - 标签闭合错误:多处将
<string>错误写成</strings,且NSLocationTemporaryUsageDescriptionDictionary对应的类型应为<dict>而非<string>,注释里的</dicts也需改为</dict>
额外必要配置与检查
- 开启精确定位权限:iOS 13及以上版本获取WiFi SSID需要精确位置权限,在Info.plist中添加:
<key>NSLocationDefaultAccuracyReduced</key> <false/>
- 代码中先请求定位权限:调用
getSSID前必须先获取定位权限,示例代码:
import { PermissionsAndroid, Platform } from 'react-native'; import WifiManager from 'react-native-wifi-reborn'; const requestLocationPermission = async () => { if (Platform.OS === 'ios') { return true; // iOS会自动触发权限弹窗,也可使用权限库处理 } else { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, { title: "Location Permission", message: "App needs access to your location to get WiFi SSID", buttonNeutral: "Ask Me Later", buttonNegative: "Cancel", buttonPositive: "OK" } ); return granted === PermissionsAndroid.RESULTS.GRANTED; } }; // 获取SSID示例 const getCurrentSSID = async () => { const hasPermission = await requestLocationPermission(); if (hasPermission) { try { const ssid = await WifiManager.getCurrentWifiSSID(); console.log('Current SSID:', ssid); } catch (error) { console.error('Failed to get SSID:', error); } } else { console.log('Location permission denied'); } };
- 检查依赖版本兼容性:确保
react-native-wifi-reborn适配RN64.0.3,建议安装^4.7.0版本:
npm install react-native-wifi-reborn@4.7.0 --save # 或 yarn add react-native-wifi-reborn@4.7.0
- 重新编译项目:修改配置和依赖后,清理缓存并重新编译:
cd ios && pod install && cd .. npx react-native run-ios --clean
内容的提问来源于stack exchange,提问作者kawsar ahmad
相关产品推荐
相关产品推荐

