业务员用的app需要限制设备登录,安卓手机比较好处理因为只需要拿imei限制就行了,
但是ios手机比较麻烦,通过驱动获取的deviceId每一次都不一样,所以需要取钥匙串
因为钥匙串是重新安装或者更新苹果系统的时候才会更新的。所以新增了一个绑定钥匙串的
插件:ionic cordova plugin add cordova-plugin-keychain-uuid
需要先运行 npm i cordova-plugin-keychain-uuid

代码如下:

            document.addEventListener("deviceready", onDeviceReady, false);
            function onDeviceReady() {
                document.addEventListener("pause", onPause, false);
                localStorage.setItem('device', device.model);
                // ios手机需要通过其他插件获取deviceID
                if (this.platform.is('ios') && localStorage.getItem('client') !== 'BROWSER') {
                    KeychainUUID.getDeviceID((id) => {
                        console.log('ios deviceId = ' + id);
                        this.zone.run(() => {
                            if (id) {
                                localStorage.setItem('imei', id);
                            }
                        });
                    }, (err) => {
                        console.log('getDeviceId error:' + JSON.stringify(err));
                    });
                } else {
                    localStorage.setItem('imei', device.uuid);
                }
            }

经过验证发现在addEventListener里面写this.platform.is('ios') 这种带this的判断是不生效的。可以在函数入口定义一个常量that来调用,不过有种更加保险一点的方式:如下面这样直接在platform启动完成就调用该插件

    initializeApp() {
        this.platform.ready().then(() => {
            if (this.platform.is('ios') && localStorage.getItem('client') !== 'BROWSER') {
                localStorage.setItem('platform', 'ios');
                this.checkAppVersion();
                KeychainUUID.getDeviceID((id) => {
                    console.log('ios deviceId + uuid = ' + id);
                    this.zone.run(() => {
                        if (id) {
                            localStorage.setItem('imei', id);
                        }
                    });
                }, (err) => {
                    console.log('getDeviceId error:' + JSON.stringify(err));
                });
                setTimeout(() => {
                    this.checkUpdate('ios');
                }, 5000);
                if ((typeof Totop) !== 'undefined') {
                    Totop.coolMethod('ToTop', success => {
                        if (success === 'ToTop') {
                            console.log("ToTop coolMethod success :" + success);
                            this.appGlobalService.topTop = true;
                            this.eventService.eventEmit.emit('TOTOP', "TOTOP");
                        }
                    }, error => {
                        console.log("test coolMethod error :" + error);
                    });
                }

                this.statusBar.overlaysWebView(true);
                this.statusBar.styleDefault();
            } else if (this.platform.is('android') && localStorage.getItem('client') !== 'BROWSER') {
                localStorage.setItem('platform', 'android');
                this.checkAppVersion();
                setTimeout(() => {
                    this.checkUpdate('android');
                }, 5000);

                this.statusBar.styleBlackTranslucent();
                this.statusBar.backgroundColorByHexString('#00000000');
            }
            // this.splashScreen.hide();
            if (this.platform.is('ios') || this.platform.is('android')) {
                localStorage.setItem('client', 'APP');
            }
            if (this.platform.is('mobileweb')) {
                localStorage.setItem('client', 'BROWSER');
            }
        });
    }

验证方法:已经打开过该app了,卸载对应的测试app之后在重新安装运行,提交post接口带的 devicedId 还是之前的。因为ios钥匙串只有重装系统才会更新。

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐