Is it enough to call $ionicPlatform.ready() once?

After reading the doc and understanding I can call $ionicPlatform.ready() pretty much anywhere, but I’m still a bit confused as to when and where I really should call ready(). Do I have to call ready() every single time before I invoke any cordova APIs? Or is calling it once in app.js good enough?

Thanks in advance.

at least you should wrap the native cordova api calls in your first app-view.
or you could use resolve states of the uirouter --> add a resolve to your initial/base state --> wait for the event to resolve the state.

Just remember your calling $ionicPlatform.ready() does not make the device ready. It’s the other way around. $ionicPlatform.ready() will resolve when the device is ready.

When an app starts, Cordova takes a while to load its code, which includes not just JS but native code as well. The camera API for example relies on the native code to be loaded else it will fail.

Now once all of this is loaded, all your cordova APIs everywhere will work.

So, if you wrap every cordova call inside deviceReady, remember that once the device actually becomes ready, all calls to deviceReady will resolve immediately without any delay.

To your question of whether you should wrap every call or just the first call, logically just the first call, but remember there is a chance that someone navigates to your controller that uses a cordova API before your device is actually ready. In that situation, your call will fail.

It depends on how you code your app. In my case, I am doing a deferred application boot-strap. I don’t have ng-app in the body of my code. I inject it inside window.ionic.Platform.ready(function() that way I know that everything is ready - otherwise I was observing some jumps in the main screen as the status bar loaded up after part of my HTML loaded.

2 Likes

Good explanation. This should go into the documentation in my opinion.

codepen please. I don’t understand.