uni-app iOS平台 uni.getLocation偶发性一直进入fail回调 重启APP后恢复正常
uni-app iOS平台 uni.getLocation偶发性一直进入fail回调 重启APP后恢复正常
| 开发环境 | 版本号 | 项目创建方式 |
|---|---|---|
| Mac | macOS Catalina 10.15.6 | HBuilderX |
产品分类:uniapp/App
PC开发环境操作系统:Mac
HBuilderX类型:正式
HBuilderX版本号:3.2.3
手机系统:iOS
手机系统版本号:IOS 14
手机厂商:苹果
手机机型:iphone 11
页面类型:vue
打包方式:云端
项目创建方式:HBuilderX
### 示例代码:
```javascript
async checkLocationAuth() {
// #ifdef APP-PLUS
// locationAuth
let result = '',
that = this;
if (plus.os.name == "iOS") {
result = permision.judgeIosPermission('location');
} else {
result = await permision.requestAndroidPermission('android.permission.ACCESS_FINE_LOCATION');
result = result == 1 ? true : result == 0 ? false : 'ban';
}
if (result) {
// 置为true 不显示定位开启提示
that.locationAuth = true;
} else {
that.locationAuth = false;
if (result != 'ban') {
uni.showToast({
title: '未开启地址定位授权',
icon: 'none'
});
} else {
uni.showToast({
title: '地址定位授权被永久拒绝',
icon: 'none'
})
}
}
// #endif
}
get_location(callback) {
uni.getLocation({
type: 'wgs84',
geocode: true,
success: function (res) {
let address = ''
if(res.address) {
address = res.address.province + ' ' + res.address.city + ' ' + res.address.district + ' ' + res.address.poiName + ' ' + res.address.street + ' ' + res.address.streetNum
}
callback({
longitude: res.longitude,
latitude: res.latitude,
poiName: res.address.poiName,
address: address
})
},
fail() { // 失败时自动唤起面板
console.log('fail');
self.locationAuth = false;
},
complete(e) {
console.log('get_location callback is', e);
}
});
}
操作步骤:
预期结果:
- 开启定位状态下, iOS系统的getLocation不要出现一直进入fail的情况
实际结果:
- 开启定位状态下, iOS系统的getLocation不要出现一直进入fail的情况
bug描述:
进入页面时先用用permision.judgeIosPermission(‘location’)判断app定位权限, (onshow里调用 checkLocationAuth方法)
确定了是开启状态 (checkLocationAuth 检测回调里把 locationAuth 自动 置为true)
但是getLocation 一直在走fail的回调 (如视频所示 点击右上角完成会触发下面贴的方法 get_location)
变量 locationAuth 是控制视频里那个定位开启提示显隐的。
我在进入fail回调的时候会在页面上显示一个开启定位的提示
重启了app之后就正常了
更多关于uni-app iOS平台 uni.getLocation偶发性一直进入fail回调 重启APP后恢复正常的实战教程也可以访问 https://www.itying.com/category-93-b0.html
更多关于uni-app iOS平台 uni.getLocation偶发性一直进入fail回调 重启APP后恢复正常的实战教程也可以访问 https://www.itying.com/category-93-b0.html
这是一个典型的iOS定位权限状态同步问题。根据你的代码和描述,问题可能出现在以下几个方面:
-
权限状态判断时机问题:
permision.judgeIosPermission('location')判断的是系统设置中的权限状态,但iOS系统在实际调用定位时可能因为系统资源、定位服务状态等原因返回失败。 -
iOS定位服务状态:除了APP权限,还需要检查系统级定位服务是否开启:
// 添加系统定位服务检查
if (!plus.location.isLocationEnabled()) {
// 系统定位服务未开启
uni.showModal({
title: '提示',
content: '请开启系统定位服务',
showCancel: false
});
return;
}
- 定位超时处理:iOS定位可能需要更长时间,建议增加超时机制:
get_location(callback) {
let timeout = setTimeout(() => {
// 超时处理
self.locationAuth = false;
callback && callback(null);
}, 10000); // 10秒超时
uni.getLocation({
type: 'wgs84',
geocode: true,
timeout: 15000, // 设置超时时间
success: function (res) {
clearTimeout(timeout);
// ... 原有成功逻辑
},
fail: function (err) {
clearTimeout(timeout);
console.log('定位失败:', err);
self.locationAuth = false;
// 可以在这里记录失败原因
if (err.errCode) {
console.error('错误码:', err.errCode, '错误信息:', err.errMsg);
}
}
});
}
- 定位失败后的重试机制:可以考虑在fail回调中添加有限次数的重试:
let retryCount = 0;
const maxRetry = 2;
function getLocationWithRetry(callback) {
uni.getLocation({
// ... 参数
fail: function(err) {
if (retryCount < maxRetry) {
retryCount++;
setTimeout(() => {
getLocationWithRetry(callback);
}, 1000);
} else {
self.locationAuth = false;
callback && callback(null);
}
}
});
}
- 检查info.plist配置:确保在manifest.json中正确配置了定位权限描述:
{
"ios": {
"permissions": {
"Location": {
"desc": "需要获取您的位置信息"
}
}
}
}

