# First Run after app installation

**URL:** <https://forum.ionicframework.com/t/first-run-after-app-installation/30204>\
**Category:** ionic-v1\
**Created:** [August 10, 2015, 7:59am UTC](https://forum.ionicframework.com/t/first-run-after-app-installation/30204 "2015-08-10T07:59:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramaa](https://avatars.discourse-cdn.com/v4/letter/r/74df32/32.png) [@ramaa](https://forum.ionicframework.com/u/ramaa)\
**Post date:** [August 10, 2015, 7:59am UTC](https://forum.ionicframework.com/t/first-run-after-app-installation/30204/1 "2015-08-10T07:59:09Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![coreelements](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@coreelements](https://forum.ionicframework.com/u/coreelements)\
**Post date:** [August 10, 2015, 8:47am UTC](https://forum.ionicframework.com/t/first-run-after-app-installation/30204/2 "2015-08-10T08:47:24Z")

</div>

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();

});
```

---

<div class="post-metadata">

**Author:** ![leob](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/leob/32/14245_2.png) [@leob](https://forum.ionicframework.com/u/leob)\
**Post date:** [August 10, 2015, 9:39am UTC](https://forum.ionicframework.com/t/first-run-after-app-installation/30204/3 "2015-08-10T09:39:05Z")

</div>

Simply use local storage? Something like this:

```auto
.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";
      }
    };
});

```

---

<div class="post-metadata">

**Author:** ![ramaa](https://avatars.discourse-cdn.com/v4/letter/r/74df32/32.png) [@ramaa](https://forum.ionicframework.com/u/ramaa)\
**Post date:** [August 10, 2015, 10:31am UTC](https://forum.ionicframework.com/t/first-run-after-app-installation/30204/4 "2015-08-10T10:31:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ramaa](https://avatars.discourse-cdn.com/v4/letter/r/74df32/32.png) [@ramaa](https://forum.ionicframework.com/u/ramaa)\
**Post date:** [August 10, 2015, 10:36am UTC](https://forum.ionicframework.com/t/first-run-after-app-installation/30204/5 "2015-08-10T10:36:34Z")

</div>

@leob thanks for this simple solution, yet its cool 😀 Any other ways to accomplish this? I don’t want to check it every n-th time to detect first run…

---

<div class="post-metadata">

**Author:** ![leob](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/leob/32/14245_2.png) [@leob](https://forum.ionicframework.com/u/leob)\
**Post date:** [August 10, 2015, 11:02am UTC](https://forum.ionicframework.com/t/first-run-after-app-installation/30204/6 "2015-08-10T11:02:32Z")

</div>

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:

```auto
.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?

---

<div class="post-metadata">

**Author:** ![ramaa](https://avatars.discourse-cdn.com/v4/letter/r/74df32/32.png) [@ramaa](https://forum.ionicframework.com/u/ramaa)\
**Post date:** [August 10, 2015, 11:37am UTC](https://forum.ionicframework.com/t/first-run-after-app-installation/30204/7 "2015-08-10T11:37:37Z")

</div>

yeah! I m gonna try on this… Thanks…

---

<div class="post-metadata">

**Author:** ![coreelements](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@coreelements](https://forum.ionicframework.com/u/coreelements)\
**Post date:** [August 10, 2015, 11:57am UTC](https://forum.ionicframework.com/t/first-run-after-app-installation/30204/8 "2015-08-10T11:57:40Z")

</div>

yes … well … in my experience it does 🙂

---

<div class="post-metadata">

**Author:** ![ramaa](https://avatars.discourse-cdn.com/v4/letter/r/74df32/32.png) [@ramaa](https://forum.ionicframework.com/u/ramaa)\
**Post date:** [September 7, 2015, 12:40pm UTC](https://forum.ionicframework.com/t/first-run-after-app-installation/30204/9 "2015-09-07T12:40:33Z")

</div>

@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 🙂

---

<div class="post-metadata">

**Author:** ![leob](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/leob/32/14245_2.png) [@leob](https://forum.ionicframework.com/u/leob)\
**Post date:** [September 7, 2015, 12:49pm UTC](https://forum.ionicframework.com/t/first-run-after-app-installation/30204/10 "2015-09-07T12:49:14Z")

</div>

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 …)

---

<div class="post-metadata">

**Author:** ![ramaa](https://avatars.discourse-cdn.com/v4/letter/r/74df32/32.png) [@ramaa](https://forum.ionicframework.com/u/ramaa)\
**Post date:** [September 7, 2015, 12:52pm UTC](https://forum.ionicframework.com/t/first-run-after-app-installation/30204/11 "2015-09-07T12:52:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![leob](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/leob/32/14245_2.png) [@leob](https://forum.ionicframework.com/u/leob)\
**Post date:** [September 7, 2015, 1:00pm UTC](https://forum.ionicframework.com/t/first-run-after-app-installation/30204/12 "2015-09-07T13:00:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ramaa](https://avatars.discourse-cdn.com/v4/letter/r/74df32/32.png) [@ramaa](https://forum.ionicframework.com/u/ramaa)\
**Post date:** [September 7, 2015, 1:06pm UTC](https://forum.ionicframework.com/t/first-run-after-app-installation/30204/13 "2015-09-07T13:06:01Z")

</div>

Thank ya @leob for clarification!!

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

---

<div class="post-metadata">

**Author:** ![leob](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/leob/32/14245_2.png) [@leob](https://forum.ionicframework.com/u/leob)\
**Post date:** [September 7, 2015, 1:14pm UTC](https://forum.ionicframework.com/t/first-run-after-app-installation/30204/14 "2015-09-07T13:14:24Z")

</div>

Just googled it … no this doesn’t exist, it’s impossible to know when your app gets uninstalled:

> <https://stackoverflow.com/questions/29317635/detect-uninstall-event-in-phonegap-app>

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

---

<div class="post-metadata">

**Author:** ![ramaa](https://avatars.discourse-cdn.com/v4/letter/r/74df32/32.png) [@ramaa](https://forum.ionicframework.com/u/ramaa)\
**Post date:** [September 7, 2015, 1:23pm UTC](https://forum.ionicframework.com/t/first-run-after-app-installation/30204/15 "2015-09-07T13:23:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![leob](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/leob/32/14245_2.png) [@leob](https://forum.ionicframework.com/u/leob)\
**Post date:** [September 7, 2015, 1:26pm UTC](https://forum.ionicframework.com/t/first-run-after-app-installation/30204/16 "2015-09-07T13:26:29Z")

</div>

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

> <https://stackoverflow.com/questions/29317635/detect-uninstall-event-in-phonegap-app>

"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"

---

<div class="post-metadata">

**Author:** ![coreelements](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@coreelements](https://forum.ionicframework.com/u/coreelements)\
**Post date:** [September 7, 2015, 9:33pm UTC](https://forum.ionicframework.com/t/first-run-after-app-installation/30204/17 "2015-09-07T21:33:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dearboy15](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/dearboy15/32/94672_2.png) [@dearboy15](https://forum.ionicframework.com/u/dearboy15)\
**Post date:** [December 13, 2015, 12:21pm UTC](https://forum.ionicframework.com/t/first-run-after-app-installation/30204/18 "2015-12-13T12:21:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![coreelements](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@coreelements](https://forum.ionicframework.com/u/coreelements)\
**Post date:** [December 13, 2015, 4:17pm UTC](https://forum.ionicframework.com/t/first-run-after-app-installation/30204/19 "2015-12-13T16:17:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jaya001](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jaya001](https://forum.ionicframework.com/u/jaya001)\
**Post date:** [March 6, 2016, 7:26am UTC](https://forum.ionicframework.com/t/first-run-after-app-installation/30204/20 "2016-03-06T07:26:16Z")

</div>

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

[Next page](https://forum.ionicframework.com/t/first-run-after-app-installation/30204.md?page=2)
