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!
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";
}
};
});
@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 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?
yeah! I m gonna try on thisā¦ Thanksā¦
yes ā¦ well ā¦ in my experience it does
@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
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"
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
HI Leob, i tried this but not working can u tell me more clear with exampleā¦