为什么 ionic-native/contacts 插件在尝试在 android 8 上保存联系人时会导致应用崩溃? [英] Why ionic-native/contacts plugin crash the app when try to save contact at android 8?
问题描述
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.
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
这篇关于为什么 ionic-native/contacts 插件在尝试在 android 8 上保存联系人时会导致应用崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!