uniapp指南针compass在苹果ios手机h5网站web上不动,安卓上面没问题的解决方法和思路

原因及解决方案:

1.uni.startCompass(),一定要绑定在事件上。不能无事件调用,即用户没有操作的情况下调用。

2.授权窗口,在调用uni.startCompass的时候,会自动弹出,无需特殊申请。

3.安卓不受影响 ,可以在无事件的情况下使用。

原因:

ios手机h5需要有方向和动作权限,这个不需要单独申请

权限弹出窗需要触发的事件,比如点击等,不能直接用代码来实现,就比如我是在onLoad里面来startCompass的,这样就不行。

解决方案:

给页面整体增加一个点击事件,事件内容开始兼听

   et_startCompass: function () {
            var that = this;
            if (that.cmbox.status == 'start') {//即已经开始了
                return false;
            }
            uni.startCompass({
                success: function (res) {
                    that.cmbox.status = 'start';
                },
                complete: function (res) {
                    that.debox.errmsg = JSON.stringify(res);
                }
            });
            uni.onCompassChange(function (res) {
                that.debox.msg = JSON.stringify(res);
                that.debox.deg = new Number(res.direction).toFixed(1);
                that.et_formatDir();
            });
        },

当使用uni.startComplass()的时候,会自动弹出此授权窗口

效果如下图: