简体   繁体   中英

How to debug native cordova plugins on ionic app?

I created a simple app with possibility to select photo from mobile phone using @ionic-native/photo-library. Now I want to debug it, print some console logs and check if photo is loaded properly:

private selectPhoto() {
    this.photoLibrary.requestAuthorization().then(() => {
        this.photoLibrary.getLibrary().subscribe({
            next: library => {
                library.forEach((libraryItem) => {
                    console.log(libraryItem) // i want to print it
                })
            },   
            error: err => {},
            complete: () => { console.log('could not get photos'); }
        })
    })
    .catch(err => console.log(err));
}

Unfortunatelly, I receive an error:

Native: tried calling PhotoLibrary.requestAuthorization, but Cordova is not available. Make sure to include cordova.js or run in a device/simulator

I read that native plugins must be tested on device but how to get output from code in my console? When I install apk I do not have any debug information.

Regards

The error means that the plugin won't properly work unless cordova.js is already loaded. This happens because cordova.js is the one responsible for loading your plugins, so if you are calling any of it before cordova is ready it just won't work.

You should use either:

document.addEventListener('deviceready', DeviceReady, false);

function DeviceReady() {
   //your plugin code here...
}

or

$ionicPlatform.ready(function() {
    //your plugin code here...
});

To properly debug an Android/iOS build with the help of a browser console (chrome/safari) make sure that you're building a debug apk/ipa with the option --debug at the CLI:

$ionic build android/ios --debug

I hope this may help you out =)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM