Sqllte Ionic : IOS device not finding database


#1

I am using Ionic to build iOS and Android apps. I want to store the user record after the first time a user logs in so that they don’t have to log in again after they close the app. So, when the app starts-up it checks if they are “Logged in” by if there is a user record in the table. If not logged in (first time they run the app or later click “log out”) then it should proceed to the login page.

this.platform.ready().then(() => { 
          this.registerBackButton();
          this.statusBar.styleDefault(); 
          this.sqlStorageService.initializeDatabase().then(()=> {  
            this.sqlStorageService.isLoggedIn().then(loggedIn => { 
              this.loader.dismiss();
              this.splashScreen.hide();
              if (loggedIn){  
                this.rootPage = HomePage;
                }else{   
                  this.rootPage = LoginPage;
                }   
            });
        }); 

In sqlStorageService:

initializeDatabase()
 { 
     return this.sqlite.create({name: 'data.db', location: 'default' }).then(db => {
         this.db = db; 
         return this.db.executeSql('CREATE TABLE IF NOT EXISTS CurrentUser(userName text primary key, firstName text, lastName text, bearerToken text, claims text)', [])
         .then(data => {console.log("initialize database complete")})
     })
 }

isLoggedIn():Promise<boolean>{
   return this.get().then(data => {
       return data != null;
   }) 
}


get() : Promise<IAppUserAuthorization> | null{  
    return this.db.executeSql('SELECT userName, firstName, lastName, bearerToken, claims FROM CurrentUser', [])
        .then(data => {   
            if (data.rows.length == 0){
                return null;
            } 

            let user: IAppUserAuthorization =
            {
                userName: data.rows.item(0).userName,
                firstName: data.rows.item(0).firstName,
                lastName: data.rows.item(0).lastName,
                bearerToken: data.rows.item(0).bearerToken,
                isAuthenticated: data.rows.item(0).isAuthenticated,
                claims: JSON.parse(data.rows.item(0).claims) 

            }  
            return user;  
    })   
}

After the user is authenticated

 return this.db.executeSql('insert or replace into CurrentUser(userName, firstName, lastName, bearerToken, claims) values(?, ?, ? , ?, ?)',
        [appUser.userName, appUser.firstName, appUser.lastName, appUser.bearerToken, JSON.stringify(appUser.claims)])
            .then(data => { 
        return JSON.parse(data.rows.item(0).value); 
      });

This all works fine when I deploy to an android device. I log in the first time. Then close the app… then reopen the app and I am taken to the home page.

When I deploy to any iOS device I log in the first time. then close the app… then reopen the app and I am taken to the login page.

I want to point out that after I log in on iOS device and I am authenticated. The record is stored in the database and I can access data in the table from any other part of the code and it works as expected. It only doesn’t work when I close the app and reopen it. It seems to not find the database (or not find the data in that table)

I have been banging my head from this for 3 days now. I’m sure its something really subtle I’m missing but I don’t get why it works flawlessly for android and not iOS