Custom uses-permission with ionic 3

2019-08-27 04:25发布

问题:

On app builds with Ionic3 (cordova 7.0.0 | android@6.2.3), I need to use microphone, SIP and internet access (WI-FI).

So I try edit config.xml with edit-config in this way:

<edit-config file="AndroidManifest.xml" mode="merge" targe="/manifest”>
<uses-permission android:name="android.permission.USE_SIP" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.webkit.PermissionRequest" />
<uses-feature android:name="android.hardware.sip.voip" />
<uses-feature android:name="android.hardware.wifi" android:required="true" />
<uses-feature android:name="android.hardware.microphone" android:required="true" />
</edit-config>

but no request is shown. Moreover, I wish that AndroidManifest.xml has more uses-permission keys (one for every request) but the result is one row with only permission for RECORD_AUDIO.

What am I doing wrong?

Thanks in advice for the answers.

回答1:

To add those permissions you need to use <config-file> not <edit-config>, for example:

    <config-file parent="/*" target="AndroidManifest.xml">
        <uses-permission android:name="android.permission.USE_SIP" />
        <uses-permission android:name="android.permission.RECORD_AUDIO" />
        <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
        <uses-permission android:name="android.webkit.PermissionRequest" />
        <uses-feature android:name="android.hardware.sip.voip" />
        <uses-feature android:name="android.hardware.wifi" android:required="true" />
        <uses-feature android:name="android.hardware.microphone" android:required="true" />
    </config-file>

But that's not enough on Android 6+ - you need to request runtime permission for dangerous permissions.

In Cordova you could do this using cordova-diagnostic-plugin with requestRuntimePermissions(), e.g.:

cordova.plugins.diagnostic.requestRuntimePermissions(function(statuses){
    for (var permission in statuses){
        switch(statuses[permission]){
            case cordova.plugins.diagnostic.permissionStatus.GRANTED:
                console.log("Permission granted to use "+permission);
                break;
            case cordova.plugins.diagnostic.permissionStatus.NOT_REQUESTED:
                console.log("Permission to use "+permission+" has not been requested yet");
                break;
            case cordova.plugins.diagnostic.permissionStatus.DENIED:
                console.log("Permission denied to use "+permission+" - ask again?");
                break;
            case cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS:
                console.log("Permission permanently denied to use "+permission+" - guess we won't be using it then!");
                break;
        }
    }
}, function(error){
    console.error("The following error occurred: "+error);
},[
    cordova.plugins.diagnostic.permission.USE_SIP,
    cordova.plugins.diagnostic.permission.RECORD_AUDIO
]);