英文:
React native permissions always return unavailable on ios simulators
问题
I have an application that uses react-native-permissions
. 基本上,我有一个关于位置的模态对话框在我的 App.js
中,只要用户没有在设备设置中选择“始终允许”选项,它就会显示。在 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(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"
请注意,这是你提供的代码的翻译。
英文:
I have an application that uses react-native-permissions
. Basically I have a modal regarding location on my App.js
and it will display as long as the user didn't select the "Always allow" option in device settings. It works well on android however modal is always showing in ios simulators knowing that I already selected the "Always allow".
Here's my code:
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);
}
I tried to console.log(permissionStatus)
and found out that it returns unavailable
Note that I already the following in my info.plist
<key>NSLocationAlwaysUsageDescription</key>
<string></string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
And in my podfile
permissions_path = '../node_modules/react-native-permissions/ios'
pod 'Permission-LocationWhenInUse', :path => "#{permissions_path}/LocationWhenInUse"
答案1
得分: 0
所以,我成功解决了问题,通过在我的 Podfile
中添加以下命令:
pod 'Permission-LocationWhenInUse', :path => "../node_modules/react-native-permissions/ios/LocationWhenInUse/Permission-LocationWhenInUse.podspec"
pod 'Permission-LocationAlways', :path => "../node_modules/react-native-permissions/ios/LocationAlways/Permission-LocationAlways.podspec"
注意: .podspec
文件有时会根据版本不同而具有不同的文件名,所以请确保在 node_modules
中检查以避免 pod install
问题。
在应用代码更改后,我运行了以下命令:
rm -rf /Library/Caches/CocoaPods && rm -rf Pods && rm -rf /Library/Developer/Xcode/DerivedData/* && cd ios && pod cache clean --all && pod deintegrate && pod setup && pod install --verbose && cd ..
然后重新运行我的项目。
英文:
So I manage to fix the issue by adding these command in my Podfile
pod 'Permission-LocationWhenInUse', :path => "../node_modules/react-native-permissions/ios/LocationWhenInUse/Permission-LocationWhenInUse.podspec"
pod 'Permission-LocationAlways', :path => "../node_modules/react-native-permissions/ios/LocationAlways/Permission-LocationAlways.podspec"
NOTE: .podspec
file sometimes have different filename depends on the version so make sure to check in node_modules
to avoid pod install
issue.
After applying the code changes I run these commands.
rm -rf /Library/Caches/CocoaPods && rm -rf Pods && rm -rf /Library/Developer/Xcode/DerivedData/* && cd ios && pod cache clean --all && pod deintegrate && pod setup && pod install --verbose && cd ..
And then re run my project.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论