Cordova plugins not working with Capacitor
Asked Answered
M

3

6

I’m not sure how to get Cordova plugins working with Ionic 4 + Capacitor in Android Emulator.

I am following this example (https://ionicacademy.com/create-pdf-files-ionic-pdfmake/) but I am going to add some Capacitor plugins to it in order to use the camera to add images.

Do the Cordova plugins below still work with Capacitor? I can’t get them to work at all. They work fine using cordova rather than Capacitor. I have created project as follows:

ionic start myApp
cd myApp
npm install @capacitor/core @capacitor/cli
npx cap init
npm install pdfmake @ionic-native/file-opener @ionic-native/file 
npm install cordova-plugin-file-opener2
npm install cordova-plugin-file

*Writes code*

create myApp/www dir
copied index.html to myApp/www dir
npx cap add android
npx cap update
npx cap open android (opens android studio)
ionic capacitor run android
npx cap sync
npx cap copy
run in emulator from android studio

Expected Result:

pdf is saved to device and is then opened.

Actual result:

No warnings or errors in console.
Fails on this on line and does not proceed:

this.file.writeFile(this.file.dataDirectory, 'myletter.pdf', blob, { replace: true }).then(fileEntry => {

Update: Warnings and error resolved but still not working

Marley answered 8/3, 2019 at 6:21 Comment(0)
R
3

Be sure to run npx cap sync after installing your cordova plugins:

https://capacitor.ionicframework.com/docs/basics/cordova

Retrochoir answered 8/3, 2019 at 6:39 Comment(3)
Oh yeah good call I should have run that rather than npx cap update. Unfortunately I tried npx cap sync now and it does not make a difference. Also tried a rebuild after.Marley
Did you do a npx cap copy after the sync?Retrochoir
Yes I tried that. I noticed it only found one of the two cordova plugins so I installed it again with npm. Reran sync and copy. I now no longer get the warnings or the error. Unfortunately it doesn't work still. Just stops at the same line of code and fails with no error.Marley
L
3

Just in case anyone is having an issue with the file plugin in capacitor and comes across this post like I did

Capacitor comes with it's own filing library that's pretty easy to use. If you're building a capacitor app and need to write and read files I'd recommend using this

https://capacitor.ionicframework.com/docs/apis/filesystem/

Luker answered 8/1, 2020 at 10:1 Comment(1)
I ended up using this. I recommend this approach.Marley
P
2

Try this command

ionic integrations disable capacitor
Phosphorescent answered 26/12, 2021 at 11:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.