在uni-app iOS上获取经纬度信息iOS手机上回报这种错误

在uni-app iOS上获取经纬度信息iOS手机上回报这种错误

开发环境 版本号 项目创建方式
Mac 15.4 HBuilderX
产品分类:uniapp/App

PC开发环境操作系统:Mac

HBuilderX类型:正式

HBuilderX版本号:4.66

手机系统:iOS

手机系统版本号:iOS 18

手机厂商:苹果

手机机型:18.5

页面类型:vue

vue版本:vue2

打包方式:云端

示例代码:

export function getUserLocationDataInfo(pageInfo: string) {
//用户名
const username = DataProviderFactory.getUsername();
return new Promise((resolve, reject) => {
uni.getLocation({
type: 'gcj02', //返回可以用于uni.openLocation的经纬度
success: async (locData) => {
resolve(locData);
},
fail: async (e) => {
resolve(e);
},
});
});
}
会报以下错误,我是升级了最新的SDK 4.66

操作步骤:

必现

预期结果:

不报以上错误,同时有什么办法可以解决

实际结果:

现在是在报错



### bug描述:
会报以下错误,我是升级了最新的SDK 4.66
This method can cause UI unresponsiveness if invoked on the main thread. Instead, consider waiting for the `-locationManagerDidChangeAuthorization:` callback and checking `authorizationStatus` first.
<Weex>[log]WXBridgeContext.mm:1323, jsLog: 进入同步状态页面 at pages/sales/vue/home/index.ts:422 __LOG
This method can cause UI unresponsiveness if invoked on the main thread. Instead, consider waiting for the `-locationManagerDidChangeAuthorization:` callback and checking `authorizationStatus` first.

更多关于在uni-app iOS上获取经纬度信息iOS手机上回报这种错误的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

可以尝试一下更新到 4.74-alpha 看看是不是能够解决这个问题

更多关于在uni-app iOS上获取经纬度信息iOS手机上回报这种错误的实战教程也可以访问 https://www.itying.com/category-93-b0.html


在uni-app iOS端获取经纬度时出现UI阻塞警告,这是因为iOS 18对定位权限检查机制进行了优化。错误提示建议在主线程中等待授权状态回调,而不是直接调用定位方法。

解决方案:

  1. 检查定位权限状态 在调用uni.getLocation前,先使用uni.authorize检查定位权限:
uni.authorize({
  scope: 'scope.userLocation',
  success: () => {
    // 已授权,执行定位
    getUserLocationDataInfo()
  },
  fail: () => {
    // 未授权,引导用户开启权限
    uni.showModal({
      content: '需要定位权限才能使用此功能',
      success: (res) => {
        if (res.confirm) {
          uni.openSetting() // 打开设置页面
        }
      }
    })
  }
})
  1. 使用异步授权检查 iOS 18要求异步处理定位授权,避免阻塞主线程:
export function getUserLocationDataInfo() {
  return new Promise((resolve, reject) => {
    // 先检查授权状态
    uni.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userLocation']) {
          // 已授权,执行定位
          uni.getLocation({
            type: 'gcj02',
            success: (locData) => resolve(locData),
            fail: (e) => reject(e)
          })
        } else {
          reject(new Error('未授权定位权限'))
        }
      }
    })
  })
}
  1. 配置manifest.json权限 确保在manifest.json中正确配置定位权限:
{
  "app-plus": {
    "distribute": {
      "ios": {
        "permissions": {
          "Location": {
            "desc": "需要获取您的位置信息"
          }
        }
      }
    }
  }
}
  1. 处理首次授权场景 首次使用时需要主动请求授权:
uni.authorize({
  scope: 'scope.userLocation'
})
回到顶部