问题
I try to save contact using ionic-native/contacts plugin
, it works on all android versions except android 8, the app is stopped and closed.
This is my code which is the same of ionic framework example:
SaveToContact(phone, name) {
try {
let contact: Contact = this.contacts.create();
contact.name = new ContactName(null, name, '');
contact.phoneNumbers = [new ContactField('mobile', phone)];
this.global.presentToast('Will Save!'),
contact.save().then(
() => this.global.presentToast('Contact saved!'),
(error: any) => this.global.presentToast('Error saving contact: No Permission')
);
} catch (e) {
this.global.presentToast(e);
}
I used android-26
to build apk, and these are the versions for ionic-native/contacts plugin
"@angular/core": "5.0.3"
"ionic-angular": "3.9.2"
"cordova-plugin-contacts": "^3.0.1"
"@ionic-native/contacts": "^4.7.0"
I also put the plugin object in the provider array in the app module, but still the app crash.
回答1:
This plugin has broken due to a change in the way Android handles permissions in 8.0 (https://developer.android.com/about/versions/oreo/android-8.0-changes#o-pri).
Basically: in the past requesting the contact write permission would also give you the contact read permission, but now you have to request both if you want to do both (but it will only ask the user once). The reason the save function was crashing the app was because it was writing the contact and then trying to read it back when it only had the write permission.
I've forked the repository and updated the ContactManager.java file to work with Android 8.0: https://github.com/duncan-c/cordova-plugin-contacts/blob/master/src/android/ContactManager.java
You can simply overwrite your ContactManager.java file in the plugins/cordova-plugin-contacts/src/android directory and then force ionic to recompile the plugin by removing and then re-adding the android platform:
>ionic platform rm android
>ionic platform add android
回答2:
Take a look at the release notes on the official cordova-plugin github. Reference
It says that they have officially deprecated the plugin as of Dec-15-2017 and they did not announce that they made the plugin android 8.0 compatible before this so we can safely assume they stopped working on the plugin before making it android 8.0 compatible.
回答3:
enter image description here
change the targetSdkVersion to 23
来源:https://stackoverflow.com/questions/51438795/why-ionic-native-contacts-plugin-crash-the-app-when-try-to-save-contact-at-andro