Capacitor: using cordova-diagnostic-plugin modules with Ionic 4 + Capacitor

Created on 27 Oct 2019  路  5Comments  路  Source: ionic-team/capacitor

The only details I can find around this issue is an unanswered question on the forum here.

The documentation for cordova-diagnostic-plugin says that you can add the following line to your config.xml to only import the modules you need from the diagnostic plugin:

<preference name="cordova.plugins.diagnostic.modules" value="[list of modules]" />

However unlike Cordova, Capacitor doesn't have the equivalent config.xml file. If I modify the file ios/App/App/config.xml to remove the additional modules, they are automatically re-added when I re-run the npx cap copy/npx cap sync commands.

The issue with this is that I have to declare additional keys in my Info.plist file requesting permission for features I am not going to use (Bluetooth for example). Once I add the keys, my app gets rejected by Apple because I am requesting permissions to use Bluetooth in the background but my binary does not use Apple's Bluetooth Module.

Does anyone have any ideas or suggestions on how to import selected modules only from the cordova-diagnostic-plugin when using Capacitor?

Most helpful comment

Create an empty config.xml file in the project root directory then add only the following line to it:

<preference name="cordova.plugins.diagnostic.modules" value="LOCATION" />

That work for me.
config.xml.zip

All 5 comments

The config.xml is generated in capacitor, but you can add your own preferences since 1.3: https://github.com/ionic-team/capacitor/pull/1999/files

After adding the follow to my capacitor.config.json file:

{
    "appId": "<APP ID>",
    "appName": "<APP NAME>",
    ...
    "cordova": {
        "preferences": {
            "cordova.plugins.diagnostic.modules": "LOCATION"
        }
    }
}

And running ionic build, npx cap sync, npx cap copy - my config.xml for iOS looks like this:

...

<feature name="Diagnostic">
    <param name="ios-package" value="Diagnostic"/>
    <param name="onload" value="true"/>
</feature>

<feature name="Diagnostic_Location">
    <param name="ios-package" value="Diagnostic_Location"/>
    <param name="onload" value="true"/>
</feature>

<feature name="Diagnostic_Bluetooth">
    <param name="ios-package" value="Diagnostic_Bluetooth"/>
    <param name="onload" value="true"/>
</feature>

...

<feature name="Diagnostic_Notifications">
    <param name="ios-package" value="Diagnostic_Notifications"/>
    <param name="onload" value="true"/>
</feature>

<preference name="cordova.plugins.diagnostic.modules" value="LOCATION" />

Am I right to think it's still including all the modules from cordova-diagnostic-plugin? I was expecting it to now only show:

<feature name="Diagnostic_Location">
    <param name="ios-package" value="Diagnostic_Location"/>
    <param name="onload" value="true"/>
</feature>

cordova-diagnostic-plugin removes the unwanted modules with a hook, Capacitor doesn't support hooks, so that's not going to work.
Since locations are different, not even that hook is going to work, but you can create something similar and run it as a node script, based on https://github.com/dpa99c/cordova-diagnostic-plugin/blob/master/scripts/apply-modules.js

Create an empty config.xml file in the project root directory then add only the following line to it:

<preference name="cordova.plugins.diagnostic.modules" value="LOCATION" />

That work for me.
config.xml.zip

I have managed to update it with the following steps:

  1. remove the plugin
  2. run npx cap sync: which will update the config file in the platform project
  3. install the plugin again - having a preference tag set to what modules I need
  4. run npx cap sync
Was this page helpful?
0 / 5 - 0 ratings

Related issues

json-derulo picture json-derulo  路  3Comments

mlynch picture mlynch  路  3Comments

bogdbo picture bogdbo  路  3Comments

json-derulo picture json-derulo  路  3Comments

stripathix picture stripathix  路  3Comments