Is there a way to display an introductory slider only once for new users when they install the app?
Solution
- If the user installs the app, set an item in the storage called
intro
with a value offalse
- When the user opens the app, check the
intro
item. If the value isfalse
, show the intro page. If it'strue
, proceed with routing and load the main page route.
Challenge
I've attempted various methods to achieve this objective, but all have failed due to the absence of an item named intro
in the storage.
Implementation
Here are my attempts so far (all located in the app.component.ts
file)
Attempt 1
import { NativeStorage } from '@ionic-native/native-storage/ngx';
constructor(
public navCtrl: NavController,
private storage: NativeStorage,
) {
this.checkIntro();
}
checkIntro(){
console.log('checking intro');
if (this.storage.getItem('intro') == undefined) {
this.storage.setItem('intro', false);
this.navCtrl.navigateRoot('/intro');
} else {
console.log('intro exists', this.storage.getItem('intro'));
}
}
Outcome: Unsuccessful.
Attempt 2
import { NativeStorage } from '@ionic-native/native-storage/ngx';
constructor(
public navCtrl: NavController,
private storage: NativeStorage,
) {
this.checkIntro();
}
checkIntro(){
console.log('checking intro');
const intro = this.storage.getItem('intro');
if (!intro){
console.log('setting intro to false in storage');
this.storage.setItem('intro', false);
this.navCtrl.navigateRoot('/intro');
} else {
this.storage.getItem('intro').then(
data => {
console.log('intro data:', data);
if(data == true){
console.log('intro already seen', data);
}else{
this.navCtrl.navigateRoot('/intro');
console.log('loading intro page first', data);
}
},
error => {
console.log('error occurred:', error);
}
)
}
}
Outcome: Unsuccessful.
Console Output
The console displays the following message (Code 2 = ITEM_NOT_FOUND):
__zone_symbol__state: 0
__zone_symbol__value: NativeStorageError
code: 2
exception: null
source: "Native"
... (truncated output for brevity)
Inquiry
How can I effectively verify the intro
item in the native local storage and direct the user based on its value?