1

我已将 Ionic Secure Storage 插件(用于存储身份验证令牌)添加到我的 Ionic 项目中,并且它在运行时在本地正常工作cordova run browser(因此cordova作为平台加载)。

但是,当我在 Android 上的 Ionic DevApp 和 Ionic View 中打开我的项目(在 iOS 上正常工作)时,每当我尝试检索保存的令牌时,它都会静默失败。

这是我的代码:

// ... unrelated imports omitted
import {Platform} from "ionic-angular";
import {SecureStorage, SecureStorageObject} from "@ionic-native/secure-storage";

@Component({
    selector: 'my-component',
    templateUrl: 'my-component.html'
})
export class MyComponent {

    constructor(private platform: Platform,
                private secureStorage: SecureStorage) {
    }

    ngOnInit() {
        this.getToken().then(token => {
            // ... do something with the retrieved token
        });
    }

    getToken() {
        if (this.platform.is('cordova')) {
            /**
             * Code below silently fails in Ionic View and Ionic Dev App
             * on Android (works correctly on iOS)
             */
            return this.platform.ready().then(() =>
                return this.secureStorage.create('cp_secure_storage')
                    .then((storage: SecureStorageObject) => {
                        return storage.get('TOKEN_NAME')
                            .then(token => {
                                console.log(token);
                                return token;
                            }, () => null);
                    });
            });
        } else {
            return Promise.resolve(localStorage.getItem('TOKEN_NAME'));
        }
    }

}

我打开了离子错误监控,它没有发现任何错误。

插件版本:

"@ionic-native/secure-storage": "4.5.3",
"cordova-plugin-secure-storage": "^2.6.8"
4

0 回答 0