expo-location 在 ios 中不起作用,并且未在应用设置中显示位置权限

Posted

技术标签:

【中文标题】expo-location 在 ios 中不起作用,并且未在应用设置中显示位置权限【英文标题】:expo-location not working in ios and not showing location permission in app settings 【发布时间】:2020-10-20 05:56:52 【问题描述】:

我正在尝试获取 ios 14 中的当前位置,但我没有得到任何响应,当我签入 expo 时 设置它没有在那里显示位置权限。我已经检查了模拟器和物理设备。

挂钩代码

import  useEffect, useState  from "react";
import * as Location from "expo-location";

export default useLocation = () => 
  const [location, setLocation] = useState();

  const getLocation = async () => 
    try 
      const  granted  = await Location.requestPermissionsAsync();
      if (!granted) return;
      const 
        coords:  latitude, longitude ,
       = await Location.getLastKnownPositionAsync();
      setLocation( latitude, longitude );
     catch (error) 
      console.log(error);
    
  ;

  useEffect(() => 
    getLocation();
  , []);

  return location;
;

回应

undefined

【问题讨论】:

刚刚也遇到了这个问题。用 Mosh 编码?我无法使该方法工作,但 Location.getCurrentPositionAsync() 方法似乎工作。 有趣的是我也遇到了同样的问题。很高兴知道我的代码没有问题。 这个问题已经为我解决了,我正在使用 Location.getCurrentPositionAsync 并且我已经卸载了该应用程序,然后重新启动了我的物理设备,然后再次安装它。它可以工作:) 【参考方案1】:

docs saysLocation.getLastKnownPositionAsync() 可能返回 null:

返回一个解析为 LocationObject 或类型对象的承诺 如果它不可用或不符合给定的要求,例如 最大年龄或要求的准确性。

所以你应该这样做:

import  useEffect, useState  from "react";
import * as Location from "expo-location";

export default useLocation = () => 
  const [location, setLocation] = useState();

  const getLocation = async () => 
    try 
      const  granted  = await Location.requestPermissionsAsync();
      if (!granted) return;
      const last = await Location.getLastKnownPositionAsync();
      if (last) setLocation(last);
      else 
        const current = await Location.getCurrentPositionAsync();
        setLocation(current);
      
     catch (error) 
      console.log(error);
    
  ;

  useEffect(() => 
    getLocation();
  , []);

  return location;
;

【讨论】:

以上是关于expo-location 在 ios 中不起作用,并且未在应用设置中显示位置权限的主要内容,如果未能解决你的问题,请参考以下文章

UIAlertController 在 iOS 9 中不起作用

setSelectedImageTintColor 在 iOS 7 中不起作用

UILocalNotification 在 iOS11 中不起作用

UIPickerView 在 IOS 8 中不起作用

ALAssetsLibraryChangedNotification 在 ios5.0 中不起作用

静默推送通知在 ios 13 中不起作用