How to register code with the deviceready event in

2019-05-22 23:39发布

问题:

I am taking over a Cordova/Ionic project. I've never worked with Cordova or Ionic before, so I am complete beginner in that area. However, I have worked with Node, on and off, for a few years, so I mostly know about that.

A simple starting task, I need to add Appsee:

https://www.appsee.com/docs/ios/ionic

This part was easy:

In case you're using TypeScript (default in ionic 2 and ionic 3) place the following line under the imports:

declare var Appsee:any;

Which I put in this file:

./src/app/app.component.ts

But this part is less obvious:

Call the following method when your app starts, preferably when the deviceready event fires:

Appsee.start("YOUR API KEY");

So I ran grep to find out where deviceready is:

grep -iR "deviceready" *   | grep -v node_modules

www/build/vendor.js:     * resolve when Cordova triggers the `deviceready` event.
www/build/vendor.js:            // prepare a custom "ready" for cordova "deviceready"
www/build/vendor.js:                    doc.addEventListener('deviceready', function () {
www/build/vendor.js:                        // 3) cordova deviceready event triggered
www/build/vendor.js:    var deviceReady = new Promise(function (resolve, reject) {
www/build/vendor.js:            document.addEventListener("deviceready", function () {
www/build/vendor.js:    var deviceReadyDone = deviceReady.catch(function () {
www/build/vendor.js:        return deviceReadyDone.then(function () {
www/build/vendor.js:    document.addEventListener('deviceready', function () {
www/build/vendor.js:        console.log("Ionic Native: deviceready event fired after " + (Date.now() - before) + " ms");
www/build/vendor.js:            console.warn("Ionic Native: deviceready did not fire within " + DEVICE_READY_TIMEOUT + "ms. This can happen when plugins are in an inconsistent state. Try removing plugins from plugins/ and reinstalling them.");                                                                    

So I only see "deviceready" inside the build folder. I think I'm supposed to avoid editing anything inside of build? Isn't that full of stuff that's generated by Ionic/Cordova?

Where do I register something with deviceready?

If I run:

ionic info 

I get:

[WARN] Detected locally installed Ionic CLI, but it's too old--using global CLI.

cli packages: (/usr/local/lib/node_modules)

    @ionic/cli-utils  : 1.19.2
    ionic (Ionic CLI) : 3.20.0

global packages:

    cordova (Cordova CLI) : 8.0.0 

local packages:

    @ionic/app-scripts : 2.1.4
    Cordova Platforms  : none
    Ionic Framework    : ionic-angular 3.6.0

System:

    ios-deploy : 1.9.2 
    Node       : v6.5.0
    npm        : 3.10.3 
    OS         : OS X El Capitan
    Xcode      : Xcode 7.3.1 Build version 7D1014 

Environment Variables:

    ANDROID_HOME : not set

Misc:

    backend : pro

I'm happy to follow directions from elsewhere.

回答1:

You should use Platform to get device ready event in IONIC, In your app.component.ts

  1. import { Platform } from 'ionic-angular';

  2. Add platform.ready() method inside the constructor as shown below

constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
    platform.ready().then(() => {
      // Okay, so the platform is ready.
      // Here you can do any higher level native things you might need.
      Appsee.start("YOUR API KEY");
      statusBar.styleDefault();
      splashScreen.hide();
    });

}

it will be triggered when device/platform is ready. Here is the documentation