如何使用我的离子2应用程序打开Android手机的安全设置?

时间:2017-05-24 11:04:30

标签: angular ionic-framework ionic2

我正在使用Ionic 2和secureStorage插件。问题是,对于Android,必须使用代码保护设备以使用安全存储。

在文档中:

var ss;
var _init = function () {
    ss = new cordova.plugins.SecureStorage(
        function () {
            console.log('OK');
        },
        function () {
            navigator.notification.alert(
                'Please enable the screen lock on your device. This app cannot operate securely without it.',
                function () {
                    ss.secureDevice(
                        function () {
                            _init();
                        },
                        function () {
                            _init();
                        }
                    );
                },
                'Screen lock is disabled'
            );
        },
        'my_app');
};
_init();

但我没有使用离子1而是使用离子2.如何调用secureDevice方法?

我做了类似的事情:

this.secureStorage.create('myStorage')
                .then((storage: SecureStorageObject) => {
                    storage.set('var', 'toto')
                        .then(
                        () => console.log('ok),
                        (e) => console.log('error');
                        );
                }).catch((err) => {
                    console.error('The device is not secured');
                })

我可以在捕获中检测到设备没有安全保护。但是如何在我的console.err旁边添加一个对secureDevice方法的调用?

文档:https://ionicframework.com/docs/native/secure-storage/

1 个答案:

答案 0 :(得分:5)

此问题为raised and fixed,因此您可以使用最新版本的@ionic-native/SecureStorage

如果您无法更新离子原生包装,请继续阅读

secureDevice函数似乎没有添加到ionic-native wrapper中,尽管它在cordova plugin中可用。

可以想到使用没有包装器的cordova插件。

ionic cordova plugin add cordova-plugin-secure-storage --save

在导入之后和类之前,立即声明对象。

declare var cordova:any;

在平台就绪()中使用the plugin api

this.platform.ready().then(() =>{
   this.ss =  new cordova.plugins.SecureStorage(
() => { console.log('Success')},
(error) => { 
   console.log('Error ' + error);
   //call here..
   this.ss.secureDevice(()=>{},()=>{});
 },
'myStorage');
});