“React Native权限始终在iOS模拟器上返回不可用”

huangapple go评论70阅读模式
英文:

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(() =&gt; {
    checkLocationPermission();
  }, []);

  const checkLocationPermission = async () =&gt; {
    const permission =
      Platform.OS === &#39;android&#39;
        ? PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION
        : PERMISSIONS.IOS.LOCATION_ALWAYS;

    const permissionStatus = await check(permission);

    if (
      permissionStatus !== RESULTS.GRANTED &amp;&amp;
      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

&lt;key&gt;NSLocationAlwaysUsageDescription&lt;/key&gt;
&lt;string&gt;&lt;/string&gt;
&lt;key&gt;NSLocationWhenInUseUsageDescription&lt;/key&gt;
&lt;string&gt;&lt;/string&gt;

And in my podfile

permissions_path = &#39;../node_modules/react-native-permissions/ios&#39;
pod &#39;Permission-LocationWhenInUse&#39;, :path =&gt; &quot;#{permissions_path}/LocationWhenInUse&quot;

答案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 &#39;Permission-LocationWhenInUse&#39;, :path =&gt; &quot;../node_modules/react-native-permissions/ios/LocationWhenInUse/Permission-LocationWhenInUse.podspec&quot; 
pod &#39;Permission-LocationAlways&#39;, :path =&gt; &quot;../node_modules/react-native-permissions/ios/LocationAlways/Permission-LocationAlways.podspec&quot;

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 &amp;&amp; rm -rf Pods &amp;&amp; rm -rf /Library/Developer/Xcode/DerivedData/* &amp;&amp; cd ios &amp;&amp; pod cache clean --all &amp;&amp; pod deintegrate &amp;&amp; pod setup &amp;&amp; pod install --verbose &amp;&amp; cd ..

And then re run my project.

huangapple
  • 本文由 发表于 2023年6月4日 23:05:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401045.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定