Issue
I'm trying to use an external library with Ionic 6 and Capacitor 4. Works fine in browsers but crashes when trying from a device. Both iOS and Android.
I insert the external library in the index.html:
index.html
<html>
<body>
<app-root></app-root>
</body>
<script src="https://urlExample.js"></script>
</html>
I declare the variable used by the library, globally in my page. I have tried declaring it with let, var or const, with the same result.
home.page.ts
import { Component } from '@angular/core';
declare var ExampleVar: any;
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
constructor() {
console.log(ExampleVar); //works fine
this.startApi();
}
startApi() {
ExampleVar.start({
authorization: 'token'
});
}
}
The ExampleVar.start function works fine, it starts making the calls that the script has.
But it doesn't seem to receive the token I send it.
This same code works fine in Capacitor 3, but when migrating to 4 it has stopped working and I don't know why.
Solution
I answer myself.
The problem came from the change in capacitor version 4.3.0: https://github.com/ionic-team/capacitor/issues/5145
From this version onwards, to allow the sending of cookies and headers in third party calls it is necessary to manage it from the capacitor.config.ts, in the root of the project.
plugins: {
CapacitorHttp: {
enabled: false,
},
CapacitorCookies: {
enabled: true,
},
}
By adding this configuration, the script calls work correctly.
Answered By - Julia Benito
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.