First Run after app installation

I have a service. I need to initialize the service only once during first run of the app after installation. How to configure it? @mhartington I ll glad to get tips on this!

2 Likes

not tested ā€¦ But I think something like this

angular.module('myModule', ['ionic'])

.run(function ($rootScope, $state, $ionicLoading , $myService) {

        // show modal
        $ionicLoading.show({
            template: 'Fetching data'
        }); 

        // do something with $myService
        //
        //
        
        // hide modal
        $ionicLoading.hide();

});

Simply use local storage? Something like this:

.factory('Application', function ($window) {
    return {
      setInitialRun = function (initial) {
          $window.localStorage["initialRun"] = (initial ? "true" : "false");
      },
      isInitialRun = function () {
         var value = $window.localStorage["initialRun"] || "true";
         return value == "true";
      }
    };
});
2 Likes

@coreelements Are you trying to say that anything given inside .run(function(){ }) will get executed exactly only once at first run after app installation?

@leob thanks for this simple solution, yet its cool :grinning: Any other ways to accomplish this? I donā€™t want to check it every n-th time to detect first runā€¦

What you mean, other way?

In the appā€™s ā€œrunā€ method you simply inject this ā€˜Applicationā€™ service that I showed and you call the ā€˜isInitialRunā€™ method.

Then if is it is true you say ā€œ$state.go(ā€˜introā€™)ā€ or something like that.
And you call ā€˜setInitialRun(false)ā€™ so next time it doesnā€™t show the intro page.

So for instance:

.run(function ($ionicPlatform, $state, Application) {
     var state = "mainPage";  // whatever, the main page of your app

     if (Application.isInitialRun()) {
           Application.setInitialRun(false);
           state = "intro";
     }

     $state.go(state);
});

Does that make sense?

2 Likes

yeah! I m gonna try on thisā€¦ Thanksā€¦

yes ā€¦ well ā€¦ in my experience it does :slight_smile:

@coreelements it s not working as you have said. app.run() gets executed each time the app is launched. So I m gonna try on @leob sā€™ suggestions. Still I donā€™t want to use localStorage for checking InitialRunā€¦ Nice & simple solution yet. Not sure if this is the right way of doing it.
I have also another issue where I have to call a service on app uninstallation. Your suggestions will be grateful :slight_smile:

Whatā€™s the problem with using localStorage then? I wouldnā€™t know how else you would do it.

(yes or you can use another storage mechanism than ā€œLocalStorageā€: SQLite, WebSQL, IndexedDB, PouchDB ā€¦ whatever, but SOME sort of local persistence/storage ā€¦)

just thought that Ionic has some built-in events to find the FirstRun

No it doesnā€™t ā€¦ you just have to do those sort of things yourself.

Itā€™s just a nice UI kit on top of AngularJS, thatā€™s all. Building a serious app still takes plenty of time and effort.

Thank ya @leob for clarification!!

Have any suggestions with you for executing a function on app uninstallation?

Just googled it ā€¦ no this doesnā€™t exist, itā€™s impossible to know when your app gets uninstalled:

So why would you want this? What do you want to do when the app is uninstalled?

In a higher level, I have subscribed to Google Cloud Messages. On installation, I will register with a Token. So, I need to notify GCM on uninstallation so that GCM can remove my Token from its list.

Okay I see ā€¦ well the Stackoverflow post that I just sent you talks about exactly this problem and has a solution for that:

"For your android app, observe the response GCM returns when you send a notification. If it sends a NotRegistered message, you can remove that id from your server. Read how unregistration works

For iOS : read this Question and its answer on SO"

1 Like

Well yeah ā€¦ Each time you do a refresh (so basicly each time the app is launched) ā€¦ .run() will run again. But itā€™s only called once ( at startup). But itā€™s not called each time you switch from state.

I think the question is about only first run after app installation.
Usually, In a mobile app, they want to show something like introduction view or anything else for the first time run only. And in this case if you put a code to .run method, it is will be called every time a user launch the app.

Then use a localstorage to store if this was the first run

1 Like

HI Leob, i tried this but not working can u tell me more clear with exampleā€¦