First Run after app installation

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…

This topic thread is almost identical to another thread: Best way to show first view dynamically when starting app

All the issues and solutions discussed there also apply here.

Anyway, initially I also couldn’t get it working, the trick was to NOT call “$urlRouterProvider.otherwise(…)” when configuring your “$stateProvider”. So, if in your app.js you have something like this:

$stateProvider

      .state('app', {
        url: "/app",
        abstract: true,
        templateUrl: "js/app/menu/menu.html"
      })

      .state('app.auth', {
        url: "/auth",
        abstract: true,
        template: '<ion-view/>'
      });

  // THE STANDARD IONIC STARTER APPS HAVE THIS LINE -
  // REMOVE THIS OR COMMENT IT OUT!
  // $urlRouterProvider.otherwise('/app/playlists');    // commented out

then make sure to REMOVE or COMMENT OUT the “$urlRouterProvider.otherwise(…)” call.

Actually this solution was already discussed in the thread I mentioned:

Best way to show first view dynamically when starting app

1 Like

Hi Leob,

it is working now… also one more i want to show ionicloading.show 2 times

$ionicLoading.show({ template: ‘Please wait…Start Sync…’ })

then code

$ionicLoading.hide()

$ionicLoading.show({ template: ‘sync completed… total user is 20.’ })

then code

$ionicLoading.hide()

first template is not showing

I implemented one for Ionic 2 using local Storage

import { Storage } from '@ionic/storage';
export class App{ 
constructor(public storage:Storage){}
     this.storage.get('launchCount').then(applaunchCount => {
        if (applaunchCount) {
          alert('launch count ' + applaunchCount);
        }
        else {
          alert('launch count '  + applaunchCount);
         this.storage.set('launchCount', applaunchCount);
        }
      });
}

HI . I am getting issue
Invalid shorthand property initializer

i used same thing and put Application in my run ,comment otherwise function . Create factory .what is wrong here ?

Hi
I added the factory ‘Application’ in app.js at the end of the chain and the app.run goes like this:
run(function($ionicPlatform,$state) {

$ionicPlatform.ready(function() {
	// Hide the accessory bar by default (remove this to show the accessory
	// bar above the keyboard
	// for form inputs)
	 <!--if(navigator && navigator.splashscreen) navigator.splashscreen.hide();-->
	if (window.cordova && window.cordova.plugins.Keyboard) {
		cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
		window.cordova.plugins.Keyboard.disableScroll(true);		}
	if (window.StatusBar) {
		// org.apache.cordova.statusbar required
		StatusBar.styleDefault();
	}

})
if (Application.isInitialRun()) {
		//alert("Initial");
       Application.setInitialRun(false);
  		$state.go('prelogin');
  		//$location.path('/loginurl');
	}
    else {
    	//alert("No");
    	//$location.path('/login');
      $state.go('login');
    }

})

The issue is that I am getting a blank white screen when the app is run.Can you please tell me where I have gone wrong?
Thanks in advance,
Sunita

Does it work in “ionic serve” but not on a device? If you run the app on an Android device you should run “adb logcat” to see what is going on, for sure there’s an error stacktrace.

Actually I am coding in Eclipse and testing in emulator.
Now I have shifted the factory ‘Application’ to services,js
This is how the app.js file starts like this:
angular.module(‘starter’,
[ ‘ionic’, ‘starter.controllers’, ‘starter.services’, ‘starter.directives’ ])
services.js is as follows:
angular.module(‘starter.services’, [‘ionic’])

.service(‘CallingService’, function($http) {
return {
. …
};
var loginUrl= localStorage.getItem(“loginurl”);
//alert(loginUrl);
return $http.post(loginUrl,logindata);
}
}
})

.service(‘PerformService’, function($http) {
return {

};
return $http.post(‘http://…’,tagdata);
}
}
})
.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”;
}
};
});

The error shown in logcat is:
Error: [$injector:nomod] Module ‘starter.services’ is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

Hi Ieob,
I found out the reason for the error:
.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”;
}
};
});

The token ‘=’ when replaced with ‘:’ solved the issue.

Ah yes, so “setInitialRun: function” instead of “setInitialRun = function” right?

Because you’re returning an object “{}” and the 2 functions are members (properties) of the object.