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…